Skip to content

Commit

Permalink
[CPP] 206. Reverse Linked List
Browse files Browse the repository at this point in the history
  • Loading branch information
UnresolvedCold committed Apr 3, 2022
1 parent 14b54f9 commit 6732c55
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions cpp/206-Reverse-Linked-List.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* 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* rev = new ListNode(0);
ListNode* ptr = head;

while(ptr) {
auto temp = ptr;
ptr=ptr->next;
temp-> next = rev->next;
rev->next = temp;
}

return rev->next;
}
};

0 comments on commit 6732c55

Please sign in to comment.