-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathremove_duplicate_in_linkedlist.java
50 lines (45 loc) · 1.38 KB
/
remove_duplicate_in_linkedlist.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
package Linkedlist;
public class remove_duplicate_in_linkedlist {
Node head;
static class Node {
int data;
Node next;
Node(int data)
{
this.data=data;
this.next=null;
}
}
public Node remove(Node head)
{ Node current=head;
while(current.next!=null){
if(current.data ==current.next.data)
{
current.next=current.next.next;
}
else {
current=current.next;
}
}
return head;
}
public void printlist(Node head)
{
Node current_node=head;
while(current_node!=null)
{
System.out.println(current_node.data);
current_node=current_node.next;
}
}
public static void main(String[] args) {
remove_duplicate_in_linkedlist ll=new remove_duplicate_in_linkedlist();
ll.head = new remove_duplicate_in_linkedlist.Node(1);
ll.head.next = new remove_duplicate_in_linkedlist.Node(1);
ll.head.next.next = new remove_duplicate_in_linkedlist.Node(2);
ll.head.next.next.next = new remove_duplicate_in_linkedlist.Node(3 );
ll.head.next.next.next.next = new remove_duplicate_in_linkedlist.Node(3);
Node t=ll.remove(ll.head);
ll.printlist(t);
}
}