Skip to content

Instantly share code, notes, and snippets.

@kanrourou
Created December 26, 2018 04:51
Show Gist options
  • Save kanrourou/223e4113bb1d23cac860e7b2eac3c478 to your computer and use it in GitHub Desktop.
Save kanrourou/223e4113bb1d23cac860e7b2eac3c478 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* deleteDuplicates(ListNode* head) {
ListNode* helper = new ListNode(-1);
helper->next = head;
auto prev = helper, curr = head;
while(curr)
{
auto next = curr;
while(next && next->val == curr->val)
next = next->next;
if(curr->next == next)
{
prev = prev->next;
curr = curr->next;
}
else
{
prev->next = next;
curr = next;
}
}
return helper->next;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment