908. Middle of the Linked List
EasySolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to find the middle node of a singly linked list. A key detail is that if the list has an even number of nodes, resulting in two middle nodes, we should return the second one. For example, in a list [1,2,3,4,5,6], the middle nodes are 3 and 4, and the correct output is node 4.
Solution
Middle of the Linked List in Python
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
# Initialize two pointers, slow and fast, at the head of the list.
slow = head
fast = head
# Traverse the list. slow moves one step at a time, fast moves two steps.
# The loop continues as long as fast and fast.next are not None.
# This condition correctly handles both odd and even length lists.
while fast and fast.next:
slow = slow.next
fast = fast.next.next
# When the loop terminates, slow is at the middle node.
# For an odd number of nodes, fast will be on the last node.
# For an even number of nodes, fast will be None.
# In both scenarios, slow points to the correct middle node.
return slow
Middle of the Linked List in Java
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode middleNode(ListNode head) {
// Initialize two pointers, slow and fast, at the head of the list.
ListNode slow = head;
ListNode fast = head;
// Traverse the list. slow moves one step at a time, fast moves two steps.
// The loop continues as long as fast and fast.next are not null.
// This condition correctly handles both odd and even length lists.
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
// When the loop terminates, slow is at the middle node.
// For an odd number of nodes, fast will be on the last node.
// For an even number of nodes, fast will be null.
// In both scenarios, slow points to the correct middle node.
return slow;
}
}
Middle of the Linked List in C++
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* middleNode(ListNode* head) {
// Initialize two pointers, slow and fast, at the head of the list.
ListNode *slow = head;
ListNode *fast = head;
// Traverse the list. slow moves one step at a time, fast moves two steps.
// The loop continues as long as fast and fast->next are not null.
// This condition correctly handles both odd and even length lists.
while (fast != nullptr && fast->next != nullptr) {
slow = slow->next;
fast = fast->next->next;
}
// When the loop terminates, slow is at the middle node.
// For an odd number of nodes, fast will be on the last node.
// For an even number of nodes, fast will be nullptr.
// In both scenarios, slow points to the correct middle node.
return slow;
}
};
Middle of the Linked List in TypeScript
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/
function middleNode(head: ListNode | null): ListNode | null {
// Initialize two pointers, slow and fast, at the head of the list.
let slow: ListNode | null = head;
let fast: ListNode | null = head;
// Traverse the list. slow moves one step at a time, fast moves two steps.
// The loop continues as long as fast and fast.next are not null.
// This condition correctly handles both odd and even length lists.
while (fast !== null && fast.next !== null) {
// The loop condition ensures slow starts non-null and moves to a non-null node,
// so the non-null assertion `!` is safe.
slow = slow!.next;
fast = fast.next.next;
}
// When the loop terminates, slow is at the middle node.
// For an odd number of nodes, fast will be on the last node.
// For an even number of nodes, fast will be null.
// In both scenarios, slow points to the correct middle node.
return slow;
};
More Easy LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.