-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemoveDuplicatesFromSortedList2.java
88 lines (76 loc) · 1.54 KB
/
RemoveDuplicatesFromSortedList2.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
public class RemoveDuplicatesFromSortedList2 {
public ListNode deleteDuplicates(ListNode head){
if(head==null || head.next==null)
return head;
while(head.next!=null)
{
if(head.val!=head.next.val)
break;
int x = head.val;
while(x==head.next.val)
{
head=head.next;
if(head.next==null)
{
return head.next;
}
}
head=head.next;
}
ListNode prevTemp = null;
ListNode prevRpt =null;
ListNode temp = head;
while(temp.next!=null)
{
if(temp.val==temp.next.val)
{
int y = temp.val;
while(y==temp.next.val)
{
temp=temp.next;
if(temp.next==null)
{
prevTemp.next=null;
return head;
}
}
prevRpt.next=temp.next;
prevTemp = prevRpt;
temp=temp.next;
continue;
}
prevTemp=temp;
prevRpt = prevTemp;
temp=temp.next;
}
return head;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
RemoveDuplicatesFromSortedList2 l = new RemoveDuplicatesFromSortedList2();
ListNode a1 = new ListNode(1);
ListNode a2 = new ListNode(2);
ListNode a3 = new ListNode(3);
ListNode a4 = new ListNode(3);
ListNode a5 = new ListNode(4);
ListNode a6 = new ListNode(4);
ListNode a7 = new ListNode(5);
a1.next = a2;
a2.next=a3;
a3.next=a4;
a4.next=a5;
a5.next=a6;
a6.next=a7;
a7.next=null;
ListNode head = l.deleteDuplicates(a1);
if(head==null)
{
System.out.println("No List");
}
while(head!=null)
{
System.out.println(head.val + " ");
head=head.next;
}
}
}