-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSolution.java
63 lines (57 loc) · 1.74 KB
/
Solution.java
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
package ds.pointer.leetcode83;
/**
* 删除排序链表中的重复元素
* LeetCode 83 https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/
*
* @author yangyi 2020年12月16日15:45:23
*/
public class Solution {
public class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
public ListNode deleteDuplicates(ListNode head) {
if (head == null) {
return null;
}
ListNode slow = head, fast = head;
while (fast != null) {
if (slow.val != fast.val) {
slow.next = fast;
slow = slow.next;
}
fast = fast.next;
}
slow.next = null;
return head;
}
public ListNode createListNode() {
ListNode val_1 = new ListNode(1);
ListNode val_1_1 = new ListNode(1);
ListNode val_2 = new ListNode(2);
ListNode val_3 = new ListNode(3);
ListNode val_3_3 = new ListNode(3);
val_1.next = val_1_1;
val_1_1.next = val_2;
val_2.next = val_3;
val_3.next = val_3_3;
return val_1;
}
public static void main(String[] args) {
Solution deleteDuplicates = new Solution();
ListNode node = deleteDuplicates.createListNode();
System.out.println("打印创建好的链表:");
for (ListNode p = node; p != null; p = p.next) {
System.out.print(p.val + "——>");
}
System.out.println();
System.out.println("打印创建好的链表:");
ListNode res = deleteDuplicates.deleteDuplicates(node);
for (ListNode p = res; p != null; p = p.next) {
System.out.print(p.val + "——>");
}
}
}