Mastering the Art of Finding the Middle Node in Linked Lists

Akhil Kumar
TheCodingWay
Published in
3 min readOct 17, 2023

In this article, we’ll dissect the middleNode function and explore how it efficiently determines the middle node of a linked list. We'll break down the code, step by step, and discuss its significance in various applications.

Understanding the middleNode Function

Let’s begin by examining the code snippet:

function middleNode(head) {
let slow = head;
let fast = head;
while (fast !== null && fast.next !== null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}

1. Initialization

The function middleNode takes a head parameter, which is the starting node of a linked list. Two pointers, slow and fast, are initialized to the head of the list. These pointers will traverse the list at different speeds.

2. The Loop

The while loop is the heart of this function. It continues as long as both fast and fast.next are not null. This condition ensures that we don't encounter errors when trying to access properties of null.

Inside the loop:

  • slow moves one step forward by assigning slow = slow.next.
  • fast moves two steps forward by assigning fast = fast.next.next.

By having one pointer move at double the speed of the other, we can efficiently find the middle of the linked list.

3. Determining the Middle Node

Once the loop terminates, the slow pointer will be positioned at the middle node of the list. This is because while fast has covered twice the distance, slow would have reached halfway.

4. Returning the Result

Finally, the function returns the slow pointer, which now points to the middle node.

Applications and Significance

The middleNode function finds extensive use in various domains:

Floyd’s Tortoise and Hare Algorithm:

  • This function implements a modified version of the Floyd’s Tortoise and Hare algorithm. It’s used to detect cycles in a linked list.

Splitting a List:

  • The middle node is a natural point to split a linked list into two halves. This is often a crucial step in sorting algorithms like merge sort.

Finding the Nth Node from the End:

  • By first finding the length of the list using a similar approach, one can easily locate the Nth node from the end.

Efficient Algorithms:

  • In scenarios where you need to perform operations on the middle element, this function provides an efficient starting point.

Conclusion

The middleNode function is a testament to the elegance and efficiency that JavaScript offers. By utilizing the power of two pointers, this function efficiently determines the middle node of a linked list.

Understanding this function not only equips you with a powerful tool for linked list operations but also exposes you to fundamental algorithmic concepts. It finds applications in a wide range of scenarios, from cycle detection to sorting.

Incorporating this knowledge into your coding arsenal will undoubtedly enhance your ability to craft efficient and elegant solutions. So, go ahead, implement and experiment with the middleNode function in your own projects!

Photo by Ryland Dean on Unsplash

Resource Leetcode 876. Middle of the Linked List

--

--