-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveDuplicatesfromSortedList2.java
More file actions
41 lines (41 loc) · 1.02 KB
/
RemoveDuplicatesfromSortedList2.java
File metadata and controls
41 lines (41 loc) · 1.02 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
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode deleteDuplicates(ListNode head) {
ListNode dummy = new ListNode(-1);
dummy.next = head;
boolean isDuplicate = false;
ListNode node = dummy;
while(node.next != null) {
if(node.next.next == null) {
break;
}
if(node.next.val != node.next.next.val) {
if(isDuplicate) {
node.next = node.next.next;
}
else {
node = node.next;
}
isDuplicate = false;
}
else {
isDuplicate = true;
node.next.next = node.next.next.next;
}
}
if(isDuplicate) {
node.next = null;
}
return dummy.next;
}
}