-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution4.java
More file actions
64 lines (57 loc) · 2.27 KB
/
Solution4.java
File metadata and controls
64 lines (57 loc) · 2.27 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
53
54
55
56
57
58
59
60
61
62
63
64
package leetcode;
// 删除排序链表中的重复元素 题目描述有问题,应该说移除相邻并且相同的元素,哎
public class Solution4 {
// public static ListNode deleteDuplicates(ListNode head) {
// Map<Integer,Integer> map = new HashMap<>();
// ListNode temp = head;
// map.put(head.val,0);
// while (temp.next!=null){
// if(map.containsKey(temp.next.val)){
// temp.next = temp.next.next;
// }else{
// map.put(temp.val,0);
// temp = temp.next;
// }
// }
// return head;
// }
public static ListNode deleteDuplicates(ListNode head) {
//创建遍历指针current,初始化指向head
ListNode current = head;
//current.next == null表示遍历的指针到达链表尾部
while(current != null && current.next != null){
//如果下一个元素的值和当前元素的值相等,就删除后后面的元素
//特殊尾部节点分析:(最后一次循环)
//相等的情况 【1 】(current)---> 【1】(current.next) 这个时候 current.next.next ==== null
if (current.next.val == current.val){
// current.next = null (直接删除的尾部的元素)
current.next = current.next.next;
} else {
//不相等的情况 【1 】(current)---> 【2】(current.next) 这个时候 current 就移动到了尾部元素
//循环结束之后 current.next 为null 即为最后一次循环
current = current.next;
}
}
return head;
}
public static void main(String[] args) {
ListNode head1 = new ListNode(1);
ListNode head2 = new ListNode(1);
ListNode head3 = new ListNode(3);
ListNode head4 = new ListNode(1);
ListNode head5 = new ListNode(5);
head1.next = head2;
head2.next = head3;
head3.next = head4;
head4.next = head5;
list(head1);
list(deleteDuplicates(head1));
}
private static void list(ListNode node){
while (node!=null){
System.out.print(node.val +"-");
node = node.next;
}
System.out.println();
}
}