#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <stack>
using namespace std;
struct Node{
int data;
Node *next;
};
Node* insert(Node* head,int x){
Node *temp1 = new Node();
temp1 -> data = x;
temp1 -> next = NULL;
if(head == NULL){
head = temp1;
return head;
}
temp1 -> next = head;
head = temp1;
return head;
}
void print(Node* head){
Node *temp = head;
while(temp != NULL){
printf("%d ",temp -> data);
temp = temp -> next;
}
printf("\n");
}
Node* Reverse(Node* head){
if(head == NULL) return head;
Node* currentNode = head;
stack<struct Node*> S;
while(currentNode != NULL){
S.push(currentNode);
currentNode = currentNode -> next;
}
currentNode = S.top();
head = currentNode;
S.pop();
while(!S.empty()){
currentNode -> next = S.top();
S.pop();
currentNode = currentNode -> next;
}
currentNode -> next = NULL;
return head;
}
Node* Reverse1(Node* head){
Node* current = head;
Node* pre = NULL;
Node* next = NULL;
while(current != NULL){
next = current -> next;
current -> next = pre;
pre = current;
current = next;
}
head = pre;
return head;
}
Node* Reverse2(Node* currentNode){
Node* head = NULL;
if(currentNode -> next == NULL){
head = currentNode;
return head;
}
head = Reverse2(currentNode -> next);
Node* nextNode = currentNode -> next;
nextNode -> next = currentNode;
currentNode -> next = NULL;
return head;
}
int main() {
Node* head = NULL;
head = insert(head,2);
head = insert(head,5);
head = insert(head,10);
head = insert(head,15);
head = insert(head,19);
print(head);
head = Reverse1(head);
print(head);
head = Reverse2(head);
print(head);
head = Reverse(head);
print(head);
}