-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathdeleteDuplicates.java
58 lines (44 loc) · 1.05 KB
/
deleteDuplicates.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
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteDuplicates(ListNode head) {
if(head == null || head.next == null)
return head;
ListNode p1 = head, p2 = head.next;
while(p2 != null)
{
if(p1.val == p2.val)
{
p2 = p2.next;
p1.next = p2;
}
else
{
p1 = p1.next;
p2 = p2.next;
}
}
return head;
}
}
class Solution {
public ListNode deleteDuplicates(ListNode head) {
ListNode cur = head;
while(cur != null)
{
if(cur.next == null)
return head;
if(cur.val == cur.next.val)
cur.next = cur.next.next;
else
cur = cur.next;
}
return head;
}
}