[LeetCode] Remove Duplicates from Sorted List II
2014-11-23 14:38
253 查看
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
For example,
Given
Given
For example,
Given
1->2->3->3->4->4->5, return
1->2->5.
Given
1->1->1->2->3, return
2->3.
class Solution { public: ListNode *deleteDuplicates(ListNode *head) { ListNode *ptr = NULL; ListNode *cur = head; ListNode *head0 = NULL; int cnt = 0; while(cur != NULL){ ListNode *tmp = cur; while(cur -> next != NULL && cur -> next -> val == cur -> val) cur = cur -> next; if(tmp == cur){ cnt ++; if(cnt == 1) head0 = ptr = cur; else { ptr -> next = cur; ptr = ptr -> next; } } cur = cur -> next; if(cnt >= 1) ptr -> next = NULL;//如出现 1 2 2 2 这种情况,所以要把ptr -> next 设为NULL; } if(cnt == 0) return NULL; return head0; } };
相关文章推荐
- [leetcode刷题系列]Remove Duplicates from Sorted List II
- leetcode 38: Remove Duplicates from Sorted List II
- LeetCode-Remove Duplicates from Sorted List II
- [LeetCode]Remove Duplicates from Sorted List II
- LeetCode: Remove Duplicates from Sorted List II
- LeetCode: Remove Duplicates from Sorted List II
- [leetcode] Remove Duplicates from Sorted List II
- LeetCode:Remove Duplicates from Sorted List II
- LeetCode 82: Remove Duplicates from Sorted List II
- [LeetCode] Remove Duplicates from Sorted List、Remove Duplicates from Sorted List II
- leetcode Remove Duplicates from Sorted List I && II
- LeetCode: Remove Duplicates from Sorted List II
- [leetcode] Remove Duplicates from Sorted List II
- [LeetCode] Remove Duplicates from Sorted List II, Solution
- [面试真题] LeetCode:Remove Duplicates from Sorted List I & II
- [leetcode]Remove Duplicates from Sorted List II
- [leetcode] Remove Duplicates from Sorted List II
- LeetCode-Remove Duplicates from Sorted List II
- LeetCode | Remove Duplicates from Sorted List II
- [Leetcode] Remove Duplicates from Sorted List II