-
Notifications
You must be signed in to change notification settings - Fork 0
/
S83.java
50 lines (44 loc) · 1.26 KB
/
S83.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
/**
* #83. Remove Duplicates from Sorted List
* https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/
*
* 解题思路:时间复杂度 O(n)
* 1 当前元素跟下个元素对比
* 2 相等移动当前元素的下个元素指针
* 3 不相等移动当前元素指针
*/
public class S83 {
public static ListNode deleteDuplicates(ListNode head) {
if(head == null) return null;
ListNode node = head;
while (node.next != null) {
if(node.val == node.next.val) {
node.next = node.next.next;
} else {
node = node.next;
}
}
return head;
}
public static class ListNode {
int val;
ListNode next;
public ListNode(int x) {
this.val = x;
}
}
public static void printAll(ListNode listNode) {
while (listNode != null) {
System.out.print(listNode.val + " ");
listNode = listNode.next;
}
}
public static void main(String[] args) {
ListNode n1 = new ListNode(1);
n1.next = new ListNode(2);
n1.next.next = new ListNode(2);
printAll(n1);
System.out.println(">>>");
printAll(deleteDuplicates(n1));
}
}