Never give up！

###### [leetcode][list] Remove Duplicates from Sorted List II

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.

For example,
Given 1->2->3->3->4->4->5, return 1->2->5.
Given 1->1->1->2->3, return 2->3.

/**
* 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) {
if (NULL == head || NULL == head->next) return head;
ListNode *p = head, *tail = NULL;//p指向当前节点，初始化为原链表第一个节点；tail指向新链表的最后一个节点，要初始化为NULL
while (p != NULL){
int val = p->val;
if (p->next == NULL || p->next->val != val){//不是重复节点
if (NULL == tail){
tail = p;
}
else{
tail->next = p;//将当前节点连入新链表
tail = tail->next;//更新tail指针
}
p = p->next;//更新当前节点指针
}
else {//是重复节点
while (p != NULL && p->val == val){//删除所有重复节点
ListNode *tmp = p;
p = p->next;
delete tmp;
}
}
}
if (tail != NULL) tail->next = NULL;//！！！新链表最后一个节点的next为NULL
}
};

#### Remove Duplicates from Sorted List -- LeetCode

2014-04-23 11:25:06

#### Remove Duplicates from Sorted List II -- LeetCode

2014-04-24 08:33:10

#### LeetCode 83 — Remove Duplicates from Sorted List（C++ Java Python）

2014-04-01 19:43:21

#### Remove Duplicates from Sorted Array II -- LeetCode

2014-04-23 08:39:29

#### LeetCode 82之Remove Duplicates from Sorted List II的Java题解

2015-05-06 17:23:34

#### 83. Remove Duplicates from Sorted List（python）

2016-09-18 21:34:35

#### LeetCode(83)Remove Duplicates from Sorted List

2014-01-10 08:06:49

#### 82. Remove Duplicates from Sorted List II Leetcode Python

2015-02-05 11:48:11

#### LeetCode 26 — Remove Duplicates from Sorted Array（C++ Java Python）

2014-02-23 15:12:20

#### leetcode || 82、Remove Duplicates from Sorted List II

2015-04-13 16:47:33

## 不良信息举报

[leetcode][list] Remove Duplicates from Sorted List II