-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRmDupInSortedList2.java
More file actions
37 lines (34 loc) · 876 Bytes
/
Copy pathRmDupInSortedList2.java
File metadata and controls
37 lines (34 loc) · 876 Bytes
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
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class RmDupInSortedList2 {
public ListNode deleteDuplicates(ListNode head) {
ListNode nil = new ListNode(-1);
nil.next = head;
ListNode last = nil;
while(head != null){
boolean dup = false;
while(head.next != null && head.val == head.next.val){
dup = true;
last.next = head.next;
head = head.next;
}
if(dup){
last.next = head.next;
head = head.next;
continue;
}
last = last.next;
head = head.next;
}
return nil.next;
}
}