Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Finding middlemost node of a linked list in JavaScript
In JavaScript, finding the middle node of a linked list is a common problem that can be efficiently solved using the two-pointer technique, also known as the "tortoise and hare" algorithm.
Problem Statement
We need to write a JavaScript function that takes the head of a linked list and returns the value of the middlemost node. If there are two middle nodes (even number of nodes), we return the second one.
For example, given the list: [4, 6, 8, 9, 1], the middle node contains the value 8.
Two-Pointer Approach
The most efficient solution uses two pointers: a slow pointer that moves one step at a time, and a fast pointer that moves two steps at a time. When the fast pointer reaches the end, the slow pointer will be at the middle.
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
class LinkedList {
constructor() {
this.head = null;
this.size = 0;
}
add(data) {
const newNode = new Node(data);
if (this.head === null) {
this.head = newNode;
} else {
let curr = this.head;
while (curr.next) {
curr = curr.next;
}
curr.next = newNode;
}
this.size++;
}
}
const findMiddle = (head) => {
let slow = head;
let fast = head;
while (fast && fast.next) {
slow = slow.next;
fast = fast.next.next;
}
return slow.data;
};
// Create and test the linked list
const list = new LinkedList();
list.add(4);
list.add(6);
list.add(8);
list.add(9);
list.add(1);
console.log("Middle node value:", findMiddle(list.head));
Middle node value: 8
How the Algorithm Works
The two-pointer technique works as follows:
- Initialize both
slowandfastpointers to the head - Move
slowone step andfasttwo steps in each iteration - When
fastreaches the end,slowis at the middle
Testing with Different List Sizes
// Test with odd number of nodes (5 nodes)
const list1 = new LinkedList();
[1, 2, 3, 4, 5].forEach(val => list1.add(val));
console.log("List [1,2,3,4,5] - Middle:", findMiddle(list1.head));
// Test with even number of nodes (4 nodes)
const list2 = new LinkedList();
[1, 2, 3, 4].forEach(val => list2.add(val));
console.log("List [1,2,3,4] - Middle:", findMiddle(list2.head));
// Test with single node
const list3 = new LinkedList();
list3.add(42);
console.log("List [42] - Middle:", findMiddle(list3.head));
List [1,2,3,4,5] - Middle: 3 List [1,2,3,4] - Middle: 3 List [42] - Middle: 42
Time and Space Complexity
| Complexity | Two-Pointer Approach | Naive Approach |
|---|---|---|
| Time | O(n) | O(n) |
| Space | O(1) | O(n) if storing nodes |
| Passes | 1 | 2 (count + traverse) |
Key Points
- The two-pointer technique requires only one pass through the list
- For even-length lists, this returns the second middle node
- No extra space is needed beyond the two pointers
- The algorithm handles edge cases like single-node lists automatically
Conclusion
The two-pointer approach efficiently finds the middle node in a single pass with constant space complexity. This technique is fundamental for many linked list problems and demonstrates the power of using multiple pointers with different speeds.
