node* reverseList(node *head){
node *prev=NULL;
node *curr=head;
node *Next=NULL;
while(curr!=NULL){
Next=curr->next;
curr->next=prev;
prev=curr;
curr=Next;
}
head=prev;
return head;
}
int main()
{
node *head=NULL;
cin>>head;
node *p=reverseList(head)
cout<<head;
cout<<endl<<“Reverse:”<<endl;
cout<<p;
/*<< and >> operators are overloaded */
}
In the above code if I insert the list as 1,2,3 and 4.
I get the output as :
1
Reverse
4
3
2
1