LeetCode OJ Remove Duplicates from Sorted List II
2015-03-23 00:40
375 查看
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinctnumbers 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 *temp; ListNode *before_temp; ListNode *new_head = head; int to_delete_int; //the specil solution of the NULL input if (head == NULL) { return NULL; } //the specil solution of the duplicates in head: while (head->next != NULL && head->val == head->next->val) { to_delete_int = head->val; ListNode *to_delete = head; while (to_delete != NULL && to_delete_int == to_delete->val) { ListNode *before_to_delete = to_delete; to_delete = to_delete->next; delete before_to_delete; } if (to_delete == NULL) { return NULL; } head = to_delete; } new_head = head; temp = head; while (temp->next != NULL) { if (temp->val == temp->next->val) { to_delete_int = temp->val; ListNode *to_delete = temp; while (to_delete != NULL && to_delete_int == to_delete->val) { ListNode *before_to_delete = to_delete; to_delete = to_delete->next; delete before_to_delete; } if (to_delete == NULL) { // if delete all the tail of the input before_temp->next = NULL; return new_head; } temp = to_delete; before_temp->next = temp; } else { before_temp = temp; temp = temp->next; } } return new_head; } };
相关文章推荐
- [leetcode]Remove Duplicates from Sorted List II @ Python
- Leetcode[82]-Remove Duplicates from Sorted List II
- 【LeetCode】Remove Duplicates from Sorted List II
- LeetCode[Linked List]: Remove Duplicates from Sorted List II
- [LeetCode-82] Remove Duplicates from Sorted List II
- LeetCode 83 Remove Duplicates from Sorted List II
- 【leetcode c++】82 Remove Duplicates from Sorted List II
- Leetcode-82: Remove Duplicates from Sorted List II
- leetcode-Remove Duplicates from Sorted List II
- Remove Duplicates from Sorted List II -- leetcode
- LeetCode-Remove Duplicates from Sorted List II
- Leetcode-Remove Duplicates from Sorted List II
- 【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系列(3)--Remove Duplicates from Sorted List II
- [LeetCode] Remove Duplicates from Sorted List II
- 【LeetCode】Remove Duplicates from Sorted List I && II
- leetcode - Remove Duplicates from Sorted List II