-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove_dupl_list.java
More file actions
80 lines (79 loc) · 2.16 KB
/
remove_dupl_list.java
File metadata and controls
80 lines (79 loc) · 2.16 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
while(cur!=null) {
while(cur!=null&&cur.val==pre.val) {
cur=cur.next;
}
if(cur==null) {
pre.next=null;
return head;
}
else {
pre.next=cur;
pre=cur;
cur=cur.next;
}
}
public class Solution {
public ListNode deleteDuplicates(ListNode head) {
// Start typing your Java solution below
// DO NOT write main() function
if(head==null||head.next==null) return head;
ListNode pre=head,cur=head.next;
while(cur!=null) {
if(cur.val==pre.val) {
pre.next=cur.next;
cur.next=null;
cur=pre.next;
}
else {
pre=cur;
cur=cur.next;
}
}
return head;
}
}
public class Solution {
public ListNode deleteDuplicates(ListNode head) {
// Start typing your Java solution below
// DO NOT write main() function
if(head==null||head.next==null) return head;
ListNode dummy=new ListNode(0);
dummy.next=head;
ListNode pre=dummy,cur=head,next=head.next;
while(next!=null) {
if(next.val==cur.val) {
while(next!=null&&next.val==cur.val) {
next=next.next;
}
if(next==null) {
pre.next=null;
return d.next;
}
pre.next=next;
cur=next;
next=next.next;
}
else {
pre=cur;
cur=next;
next=next.next;
}
}
return dummy.next;
}
}
while(next!=null) {
if(next.val==cur.val) {
while(next!=null&&next.val==cur.val) {
next=next.next;
}
pre.next=next;
cur=next;
if(next!=null) next=next.next;
}
else {
pre=cur;
cur=next;
next=next.next;
}
}