Skip to content

Commit

Permalink
Create 206.Reverse-Linked-List.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
wisdompeak authored Nov 17, 2021
1 parent 659b26e commit 9f0732c
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions Linked_List/206.Reverse-Linked-List/206.Reverse-Linked-List.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head)
{
ListNode* last = NULL;
ListNode* cur = head;
ListNode* nxt;

while (cur)
{
nxt = cur->next;
cur->next = last;
last = cur;
cur = nxt;
}
return last;
}
};

0 comments on commit 9f0732c

Please sign in to comment.