-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathsolution.js
71 lines (66 loc) · 1.61 KB
/
solution.js
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
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 currentNode = this.head;
while (currentNode.next) {
currentNode = currentNode.next;
}
currentNode.next = newNode;
}
}
//Function to print the singlely linkedlist.
printList() {
let currentNode = this.head;
let list = "";
while (currentNode) {
list += currentNode.data + "->";
currentNode = currentNode.next;
}
list += "null";
console.log(list);
}
//Function to remove dupliicate nodes from the singlely linkedlist.
removeDuplicates() {
let currentNode1;
currentNode1 = this.head;
while (currentNode1 && currentNode1.next) {
let currentNode2 = currentNode1;
while (currentNode2.next) {
if (currentNode1.data == currentNode2.next.data) {
currentNode2.next = currentNode2.next.next;
} else {
currentNode2 = currentNode2.next;
}
}
currentNode1 = currentNode1.next;
}
}
}
//main function.
var list1 = new LinkedList();
list1.insert(0);
list1.insert(1);
list1.insert(2);
list1.insert(3);
list1.insert(1);
list1.insert(4);
list1.insert(4);
console.log("Original List with Duplicates:");
list1.printList();
list1.removeDuplicates();
console.log("********************************");
console.log("New List free from Duplicates:");
list1.printList();