Problem
Submissions

Solution: Middle of the Linked List

Statement

Naive approach

In the naive approach, we use an external array to store the elements of the linked list, and then we return the element present at the index ⌊array.length2⌋\lfloor \frac{array.length}{2} \rfloor as the middle node of the linked list. The time and space complexity of this approach is O(n)O(n), where nn is the number of nodes in the linked list. Let’s see if we can solve this problem with better time and space complexity.

Optimized approach using fast and slow pointers

Problem
Submissions

Solution: Middle of the Linked List

Statement

Naive approach

In the naive approach, we use an external array to store the elements of the linked list, and then we return the element present at the index ⌊array.length2⌋\lfloor \frac{array.length}{2} \rfloor as the middle node of the linked list. The time and space complexity of this approach is O(n)O(n), where nn is the number of nodes in the linked list. Let’s see if we can solve this problem with better time and space complexity.

Optimized approach using fast and slow pointers