class Solution { public: //Function to reverse a linked list. struct Node* reverseList(struct Node *head) { if(head->next==NULL) return head; Node *l,*t,*f; l=head,t=head->next,f=head->next->next; head->next=NULL; while(t->next) { t->next=l; l=t; t=f; f=f->next; } t->next=l; head=t; return head; // code here // return head of reversed list } };
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter