-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListNode2.java
More file actions
55 lines (47 loc) · 1.33 KB
/
Copy pathListNode2.java
File metadata and controls
55 lines (47 loc) · 1.33 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* Given a sorted linked list, delete all nodes that
* have duplicate numbers, leaving only distinct
* numbers from the original list.
* Example
* Given 1->2->3->3->4->4->5, return 1->2->5.
* Given 1->1->1->2->3, return 2->3.
**/
public class ListNode2 {
int val;
ListNode2 next;
ListNode2(int x) {
val = x;
next = null;
}
public static ListNode2 deleteDuplicates(ListNode2 head) {
// write your code here
if ((head == null) || (head.next == null)) {
return head;
}
ListNode2 dummy = new ListNode2(1);
dummy.next = head;
head = dummy;
while (head.next != null) {
if (head.next.val == head.next.next.val) {
head.next = head.next.next;
} else {
head = head.next;
}
}
return head;
}
public static void main(String[] args) {
/*
ListNode2 node0 = new ListNode2(1);
ListNode2 node1 = new ListNode2(1);
ListNode2 node2 = new ListNode2(2);
ListNode2 node3 = new ListNode2(3);
node0.next = node1;
node1.next = node2;
node2.next = node3;
// node3.next = null;
// node0 = deleteDuplicates(node0);
System.out.print(node0.val + "->" + node1.val + "->" + node2.val + "->" + node3.val);
*/
}
}