Skip to content

Commit fe801dc

Browse files
authoredNov 22, 2021
Create 206.Reverse-Linked-List_Recursion.cpp
1 parent a618066 commit fe801dc

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
 
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode() : val(0), next(nullptr) {}
7+
* ListNode(int x) : val(x), next(nullptr) {}
8+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9+
* };
10+
*/
11+
class Solution {
12+
public:
13+
ListNode* reverseList(ListNode* head)
14+
{
15+
return helper(NULL, head);
16+
}
17+
18+
ListNode* helper(ListNode*last, ListNode* head)
19+
{
20+
if (head==NULL) return last;
21+
ListNode* nxt = head->next;
22+
head->next = last;
23+
return helper(head, nxt);
24+
}
25+
};

0 commit comments

Comments
 (0)
Please sign in to comment.