Skip to content

Commit 47d879d

Browse files
authored
Create 206. Reverse Linked List (#435)
2 parents 4271502 + 000bfd8 commit 47d879d

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

206. Reverse Linked List

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
if(!head)return head;
15+
ListNode* prev=NULL;
16+
ListNode* curr=head;
17+
ListNode* leader;
18+
19+
while(curr!=NULL){
20+
leader=curr->next;
21+
curr->next=prev;
22+
prev=curr;
23+
curr=leader;
24+
}
25+
26+
return prev;
27+
}
28+
};

0 commit comments

Comments
 (0)