-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathsolution.js
More file actions
87 lines (68 loc) · 1.28 KB
/
solution.js
File metadata and controls
87 lines (68 loc) · 1.28 KB
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
class node{
constructor(data){
this.data=data;
this.next=null;
}
}
class LinkedList{
constructor(){
this.head=null;
}
//Function to insert a node in the singlely linkedlist.
insert(data){
var newnode=new node(data);
if(this.head==null){
this.head=newnode;
}
else{
var temp=this.head;
while(temp.next){
temp=temp.next;
}
temp.next=newnode;
}
}
//Function to print the singlely linkedlist.
printList(){
var temp=this.head;
var list="";
while(temp){
list+=temp.data+"->";
temp=temp.next;
}
list+="null";
console.log(list);
}
//Function to remove dupliicate nodes from the singlely linkedlist.
removeDuplicates(){
var temp1,temp2;
temp1=this.head;
while(temp1 && temp1.next){
temp2=temp1;
while(temp2.next){
if(temp1.data==temp2.next.data){
temp2.next=temp2.next.next;
}
else{
temp2=temp2.next;
}
}
temp1=temp1.next;
}
}
}
//main function.
/*var list_1=new LinkedList();
list_1.insert(0);
list_1.insert(1);
list_1.insert(2);
list_1.insert(3);
list_1.insert(1);
list_1.insert(4);
list_1.insert(4);
console.log("Original List with Duplicates:");
list_1.printList();
list_1.removeDuplicates();
console.log("New List free of duplicates:");
list_1.printList();
*/