Skip to content

Commit 1fdf0fa

Browse files
[CPP] 206. Reverse Linked List
1 parent 6e9b697 commit 1fdf0fa

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

cpp/206-Reverse-Linked-List.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
ListNode* rev = new ListNode(0);
15+
ListNode* ptr = head;
16+
17+
while(ptr) {
18+
auto temp = ptr;
19+
ptr=ptr->next;
20+
temp-> next = rev->next;
21+
rev->next = temp;
22+
}
23+
24+
return rev->next;
25+
}
26+
};

0 commit comments

Comments
 (0)