Skip to content

Instantly share code, notes, and snippets.

@s4553711
Created April 4, 2017 03:51
Show Gist options
  • Save s4553711/f1b0d34fe9a6002313e508705aa68436 to your computer and use it in GitHub Desktop.
Save s4553711/f1b0d34fe9a6002313e508705aa68436 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 == NULL || head->next == NULL) {
return head;
}
ListNode* prev = NULL;
ListNode* curr = head;
ListNode* preceding = head->next;
while (preceding != NULL) {
curr->next = prev;
prev = curr;
curr = preceding;
preceding = preceding->next;
}
curr->next = prev;
return curr;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment