-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathRmDuplicateSorted.java
59 lines (58 loc) · 1.48 KB
/
RmDuplicateSorted.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
package SummerTrainingGFG.LinkedList;
/**
* @author Vishal Singh
*/
public class RmDuplicateSorted {
static class Node{
int data;
Node next;
Node(int data){
this.data = data;
next = null;
}
}
static class List{
Node head;
Node tail;
void addToTheLast(Node node){
if(head == null){
head = node;
tail = node;
}
else{
tail.next = node;
tail = node;
}
}
void print(){
Node curr = head;
while(curr != null){
System.out.print(curr.data+",");
curr = curr.next;
}
System.out.println("");
}
void removeDuplicates() {
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 main(String[] args) {
List l = new List();
l.addToTheLast(new Node(2));
l.addToTheLast(new Node(3));
l.addToTheLast(new Node(3));
l.addToTheLast(new Node(4));
l.addToTheLast(new Node(55));
l.addToTheLast(new Node(556));
l.addToTheLast(new Node(556));
l.print();
l.removeDuplicates();
l.print();
}
}