Middle of the Linked List — Solution in Python, Java & C++
Middle of the Linked List solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem on two pointers.
Middle of the Linked List is an easy coding problem on two pointers, asked in Amazon, Microsoft and Adobe interviews.
- Difficulty: Easy
- Topics: Two Pointers
- Asked at: Amazon, Microsoft, Adobe
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
Given the head of a non-empty singly linked list, return its middle node — the list from that node to the end is what gets printed.
When the list has two middle nodes (an even number of nodes), return the second one.
Example 1
Input: head = [1,2,3,4,5]
Output: [3,4,5]
Explanation: The middle node is 3.
Example 2
Input: head = [1,2,3,4,5,6]
Output: [4,5,6]
Explanation: 3 and 4 are both middle nodes; the second one, 4, is returned.
Example 3
Input: head = [7]
Output: [7]
Constraints
The number of nodes in the list is in the range [1, 100]1 <= Node.val <= 100
Hints for Middle of the Linked List
Hint 1
Counting the nodes first and then walking half way works in two passes.
Hint 2
Can one pass do it? Move one pointer one step at a time and another two steps at a time.
Hint 3
When the fast pointer cannot take two more steps (it is null, or its next is null), the slow pointer is on the (second) middle node.
How to solve Middle of the Linked List
A pointer moving at twice the speed reaches the end exactly when the slower one reaches the middle.
Approach
- Set
slow = fast = head. - While
fastandfast.nextare not null:slow = slow.next,fast = fast.next.next. - Return
slow.
Why it works
After k iterations slow is at index k and fast at index 2k. The loop stops at the first k with 2k >= n - 1, i.e. k = floor(n / 2) — the middle for odd n and the second middle for even n.
Complexity
- Time —
O(n) - Space —
O(1)
Pitfalls
- Testing
fast.next.nextinstead offast.nextin the loop condition returns the first middle on even lengths. - Check
fastbeforefast.nextto avoid dereferencing null.
Reference solution
Python
from typing import Optional
def middleNode(head: Optional[ListNode]) -> Optional[ListNode]:
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
return slowJava
public static ListNode middleNode(ListNode head) {
ListNode slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}C++
ListNode* middleNode(ListNode* head) {
ListNode *slow = head, *fast = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
}
return slow;
}JavaScript
var middleNode = function(head) {
var slow = head, fast = head;
while (fast !== null && fast.next !== null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 165 two pointers problems · the whole catalogue
Learn the technique: Two Pointers Technique