-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path083_RemoveDuplicatesFromSortedList83.java
68 lines (61 loc) · 1.73 KB
/
083_RemoveDuplicatesFromSortedList83.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
64
65
66
67
68
/**
* Given a sorted linked list, delete all duplicates such that each element
* appear only once.
*
* Example 1:
* Input: 1->1->2
* Output: 1->2
*
* Example 2:
* Input: 1->1->2->3->3
* Output: 1->2->3
*/
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class RemoveDuplicatesFromSortedList83 {
public ListNode deleteDuplicates(ListNode head) {
if (head == null || head.next == null) return head;
ListNode fast = head.next;
ListNode slow = head;
slow.next = null;
while (fast != null) {
if (fast.val == slow.val) {
fast = fast.next;
} else {
slow.next = fast;
fast = fast.next;
slow = slow.next;
slow.next = null;
}
}
return head;
}
/**
* https://leetcode.com/problems/remove-duplicates-from-sorted-list/solution/
*/
public ListNode deleteDuplicates2(ListNode head) {
ListNode current = head;
while (current != null && current.next != null) {
if (current.next.val == current.val) {
current.next = current.next.next;
} else {
current = current.next;
}
}
return head;
}
/**
* https://leetcode.com/problems/remove-duplicates-from-sorted-list/discuss/28625/3-Line-JAVA-recursive-solution
*/
public ListNode deleteDuplicates3(ListNode head) {
if (head == null || head.next == null) return head;
head.next = deleteDuplicates3(head.next);
return head.val == head.next.val ? head.next : head;
}
}