-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathremoveDupliInSortedLL.java
52 lines (47 loc) · 1.25 KB
/
removeDupliInSortedLL.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
/**
* Given a sorted SLL, rmeove duplicates from the list
* ip: 10->20->20->30->30->30->null
* op: 10->20->30
*
* ip: null
* op: null
*
* Approach:
* Traverse the LL using a curr ref/ptr
* Check if curr's data === curr's next data
* if yes
* remove the node
* else
* move curr to next node
*/
public class removeDupliInSortedLL {
public static void main(String[] args) {
Node head = new Node(10);
head.next = new Node(20);
head.next.next = new Node(20);
head.next.next.next = new Node(30);
head.next.next.next.next = new Node(30);
head.next.next.next.next.next = new Node(30);
printList(head);
removeDupli(head);
printList(head);
}
public static void removeDupli(Node head) {
Node curr = head;
while(curr!=null && curr.next!=null)
{
if(curr.data == curr.next.data)
curr.next = curr.next.next;
else
curr = curr.next;
}
}
public static void printList(Node head) {
Node traverse = head;
while (traverse != null) {
System.out.print(traverse.data + "->");
traverse = traverse.next;
}
System.out.print("null\n");
}
}