Skip to content

Commit 9f0732c

Browse files
authored
Create 206.Reverse-Linked-List.cpp
1 parent 659b26e commit 9f0732c

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
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+
{
15+
ListNode* last = NULL;
16+
ListNode* cur = head;
17+
ListNode* nxt;
18+
19+
while (cur)
20+
{
21+
nxt = cur->next;
22+
cur->next = last;
23+
last = cur;
24+
cur = nxt;
25+
}
26+
return last;
27+
}
28+
};

0 commit comments

Comments
 (0)