Skip to content

Instantly share code, notes, and snippets.

@walkingtospace
Created August 27, 2014 00:45
Show Gist options
  • Save walkingtospace/05c4866cf407ebb64f71 to your computer and use it in GitHub Desktop.
Save walkingtospace/05c4866cf407ebb64f71 to your computer and use it in GitHub Desktop.
remove-duplicates-from-sorted-list
https://oj.leetcode.com/problems/remove-duplicates-from-sorted-list/
/*
반드시 인접해있는 두 노드만 duplicated 되어있는지? -> YES
O(n) solution : 순회하면서 duplicated 노드 만나면 삭제(건너뛰고)연결
*/
/**
* 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 * cur = head;
while(cur) {
ListNode* next = cur->next;
while(next && cur->val == next->val) {
delete next;
next = next->next;
}
cur->next = next;
cur = next;
}
return head;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment