-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindMidSinglyLinkedList.js
68 lines (60 loc) · 1.59 KB
/
findMidSinglyLinkedList.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
//This is a possible solution to find a middle element of a singly linked list
// TODO: This may need some refactoring. I need to think about this solution.
class Node {
constructor(data)
{
this.data = data;
this.next = null
}
}
// Linked List class
class LinkedList {
constructor()
{
this.head = null;
this.size = 0;
}
// Add a new node at the end
add(data)
{
// Create a new node
let node = new Node(data);
// Store current node
let current;
// If list is Empty, add the node and make it head
if (this.head == null)
this.head = node;
else {
current = this.head;
// Iterate to the end of the list
while (current.next) {
current = current.next;
}
// Add node
current.next = node;
}
this.size++;
}
// Find the middle element of the list
findMiddle()
{
let slowPtr = this.head;
let fastPtr = this.head;
if (this.head !== null) {
while (fastPtr !== null && fastPtr.next !== null) {
fastPtr = fastPtr.next.next;
slowPtr = slowPtr.next;
}
console.log("The middle element is: " + slowPtr.data);
}
}
}
//Create an instance of the Linked List
let list = new LinkedList();
list.add(10);
list.add(20);
list.add(30);
list.add(40);
list.add(50);
//Find the middle element of the list
list.findMiddle();