Skip to content

Commit bb80bf0

Browse files
authored
Create 206_Reverse_Linked_List.cpp
1 parent 3564604 commit bb80bf0

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

cpp/206_Reverse_Linked_List.cpp

+26
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(int x) : val(x), next(NULL) {}
7+
* };
8+
*/
9+
class Solution {
10+
public:
11+
ListNode* reverseList(ListNode* head) {
12+
if(head==NULL)return NULL;
13+
if(head->next==NULL) return head;
14+
ListNode* p=head;
15+
ListNode* c=head->next;
16+
head->next=NULL;
17+
while(c->next){
18+
ListNode* n = c->next;
19+
c->next=p;
20+
p=c;
21+
c=n;
22+
}
23+
c->next=p;
24+
return c;
25+
}
26+
};

0 commit comments

Comments
 (0)