Skip to content

Instantly share code, notes, and snippets.

@kanrourou
Created December 25, 2018 19:32
Show Gist options
  • Select an option

  • Save kanrourou/45fea53f80b7ad52a16d0adbcfaae208 to your computer and use it in GitHub Desktop.

Select an option

Save kanrourou/45fea53f80b7ad52a16d0adbcfaae208 to your computer and use it in GitHub Desktop.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(!head)return head;
ListNode* curr = head->next, *prev = head;
prev->next = nullptr;
while(curr)
{
ListNode* tmp = curr->next;
curr->next = prev;
prev = curr;
curr = tmp;
}
return prev;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment