-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
34 lines (33 loc) · 948 Bytes
/
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
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteDuplicates(ListNode head) {
ListNode node = head,
tempNode = null;
head = null;
while (node != null) {
boolean hasDuplicate = false;
while (node.next != null && node.val == node.next.val) {
hasDuplicate = true;
node = node.next;
}
if (hasDuplicate == false) {
if (tempNode == null) {
tempNode = new ListNode(node.val);
head = tempNode;
} else {
tempNode.next = new ListNode(node.val);
tempNode = tempNode.next;
}
}
node = node.next;
}
return head;
}
}