Skip to content

Commit d0b9e81

Browse files
Create Reverse Linked List.cpp
1 parent 1cf5d9b commit d0b9e81

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

LeetCode/Reverse Linked List.cpp

+28
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+
12+
//recursive
13+
class Solution {
14+
public:
15+
ListNode* reverseList(ListNode* head) {
16+
ListNode*C = head;
17+
ListNode*P = NULL;
18+
ListNode*N;
19+
while(C!=NULL){
20+
N = C->next;
21+
C->next = P;
22+
P = C;
23+
C= N;
24+
}
25+
head = P;
26+
return head;
27+
}
28+
};

0 commit comments

Comments
 (0)