Created
August 27, 2014 00:45
-
-
Save walkingtospace/05c4866cf407ebb64f71 to your computer and use it in GitHub Desktop.
remove-duplicates-from-sorted-list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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