# 【Leetcode】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.

	public ListNode deleteDuplicates(ListNode head) {
return null;
// pre p q
q = p.next;
while (q != null) {
if (p.val == q.val) {
while (q != null && p.val == q.val) {// 如果节点相等则前移至后面第一个不等节点
q = q.next;
}
pre.next = q;
p = pre;//记得把p要更新，否则p指向的是要删除的结点,下次循环进入else会出错。p不能更新为q，否则下轮循环p/q相等进入死循环。
} else {
pre = p;
p = q;
q = q.next;
}
}
}

	public ListNode deleteDuplicates(ListNode head) {
ListNode p = head, q, pre = null;
if (p == null) {
}
// pre p q
q = p.next;
while (q != null) {
if (p.val == q.val) {
while (q != null && p.val == q.val) {// 如果节点相等则前移至第一个不等节点
q = q.next;
}
if (pre == null) {// 如果头节点重复,需要特殊处理
if (q != null) // 若不是1,1这种情况，即q为null时，q向前移到第二个2位置
q = q.next;
} else { //
pre.next = q;
p = pre;
}
} else {
pre = p;
p = q;
q = q.next;
}
}
}

#### 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

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

2015-02-05 11:48:11

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

2014-04-23 08:39:29

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

2014-04-01 19:43:21

#### [leetcode]Remove Duplicates from Sorted List (删除有序节点的值重复的节点 C语言)

2015-03-28 18:17:40

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

2016-09-18 21:34:35

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

2014-02-23 15:12:20

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

2015-05-06 17:23:34

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

2014-01-10 08:06:49