-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathduplctLL.java
74 lines (69 loc) · 1.73 KB
/
duplctLL.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
import java.util.*;
public class duplctLL {
static Node head;
class Node{
int data;
Node next;
Node(int data){
this.data=data;
this.next=null;
}
}
public static void duprm(Node head){
Node n=new Node();
n=head;
Node temp=n.next;
while(n!=null){
int p=n.data;
if(temp.data==p){
delete(temp);
}else{
temp=temp.next;
}
n=n.next;
}
return;
}
public void delete(Node nodetodel){
if(nodetodel==null||head==null){
return;
}
else if(nodetodel==head){
head=nodetodel.next;
return;
}
Node previous=head;
while(previous.next!=nodetodel){
previous=previous.next;
}
if(previous.next==nodetodel){
previous.next=previous.next.next;
return;
}
if(previous.next==null){
return;
}
}
public static void main(String[] args) {
duplctLL linkedList = new duplctLL();
linkedList.addNode(1);
linkedList.addNode(2);
linkedList.addNode(3);
linkedList.addNode(2);
linkedList.addNode(4);
linkedList.addNode(1);
linkedList.duprm();
}
public void addNode(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
} else {
Node temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = newNode;
}
}
}