-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeleteDuplication.java
More file actions
52 lines (48 loc) · 1.62 KB
/
Copy pathDeleteDuplication.java
File metadata and controls
52 lines (48 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package linkedList;
import org.junit.Test;
/**
* @Author: wei1
* @Date: Create in 2019/1/4 23:20
* @Description: 题目描述
* 在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,
* 重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5
* 处理后为 1->2->5
*/
public class DeleteDuplication {
public ListNode deleteDuplication(ListNode pHead) {
if (pHead == null || pHead.next == null) {
return pHead;
}
ListNode p = new ListNode(0);
ListNode head = p;
while (pHead != null && pHead.next != null) {
if (pHead.val == pHead.next.val) {
int v = pHead.val;
while (pHead != null && pHead.val == v) {
pHead = pHead.next;
}
p.next = pHead;
} else {
p.next = pHead;
p = p.next;
pHead = pHead.next;
}
}
return head.next;
}
@Test
public void test() {
ListNode listNode = new ListNode(1);
listNode.next = new ListNode(1);
// listNode.next.next = new ListNode(3);
// listNode.next.next.next = new ListNode(3);
// listNode.next.next.next.next = new ListNode(4);
// listNode.next.next.next.next.next = new ListNode(4);
// listNode.next.next.next.next.next.next = new ListNode(5);
ListNode listNode1 = deleteDuplication(listNode);
while (listNode1 != null) {
System.out.println(listNode1.val);
listNode1 = listNode1.next;
}
}
}