Skip to content

Commit

Permalink
Create 206.Reverse-Linked-List_Recursion.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
wisdompeak authored Nov 22, 2021
1 parent a618066 commit fe801dc
Showing 1 changed file with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head)
{
return helper(NULL, head);
}

ListNode* helper(ListNode*last, ListNode* head)
{
if (head==NULL) return last;
ListNode* nxt = head->next;
head->next = last;
return helper(head, nxt);
}
};

0 comments on commit fe801dc

Please sign in to comment.