All suggestions are welcome. Please upvote if you like it. Thank you.
Leetcode Problem Link: 206. Reverse Linked List
Using Recursion:
class Solution { public: ListNode* reverseList(ListNode* head) { if(!head || !head->next) return head; ListNode *ptr=reverseList(head->next); head->next->next=head; head->next=NULL; head=ptr; return head; } };
Using Iteration:
class Solution { public: ListNode* reverseList(ListNode* head) { ListNode *prev=NULL, *curr=head, *next=NULL; while(curr){ next=curr->next; curr->next=prev; prev=curr; curr=next; } head=prev; return head; } };
All suggestions are welcome. Please upvote if you like it. Thank you.
Source: Leetcode premium solution editorial
Top comments (2)
Awesome!
Thank you George for the compliment.