Skip to main content

Delete the Middle Node of a Linked List - Solution & Explanation

MediumLinked ListTwo Pointers18 min readAsked at: Amazon, Microsoft, Goldman Sachs +4
Practice this problem

Problem Statement

You are given the head of a linked list. Delete the middle node, and return the head of the modified linked list.

The middle node of a linked list of size n is the ⌊n / 2⌋th node from the start using 0-based indexing, where ⌊x⌋ denotes the largest integer less than or equal to x.

  • For n = 1, 2, 3, 4, and 5, the middle nodes are 0, 1, 1, 2, and 2, respectively.

 

Example 1:

Input: head = [1,3,4,7,1,2,6]
Output: [1,3,4,1,2,6]
Explanation:
The above figure represents the given linked list. The indices of the nodes are written below.
Since n = 7, node 3 with value 7 is the middle node, which is marked in red.
We return the new list after removing this node. 

Example 2:

Input: head = [1,2,3,4]
Output: [1,2,4]
Explanation:
The above figure represents the given linked list.
For n = 4, node 2 with value 3 is the middle node, which is marked in red.

Example 3:

Input: head = [2,1]
Output: [2]
Explanation:
The above figure represents the given linked list.
For n = 2, node 1 with value 1 is the middle node, which is marked in red.
Node 0 with value 2 is the only node remaining after removing node 1.

 

Constraints:

  • The number of nodes in the list is in the range [1, 105].
  • 1 <= Node.val <= 105

Approach Overview

Problem Overview: You receive the head of a singly linked list and must delete the middle node, returning the updated list. The middle is defined as the ⌊n / 2⌋ index (0‑based). If the list has only one node, deleting the middle results in an empty list.

This problem tests your ability to traverse a linked list efficiently and manipulate node pointers without extra memory. The challenge is identifying the middle node and updating the previous node’s next pointer correctly.

Approach 1: Two-Pass Method (O(n) time, O(1) space)

Traverse the linked list once to compute its length. The middle index is n // 2. Perform a second traversal to reach the node just before the middle. Update its next pointer to skip the middle node (prev.next = prev.next.next). This approach is straightforward because you explicitly know the target index before deletion. The downside is two full passes over the list, but the complexity still remains linear with constant extra memory.

The main operations are simple pointer iteration and index tracking. If the list length is 1, return null. Otherwise, stop at index (n // 2) - 1 and rewire the pointer. This method is easy to reason about and useful when interviewers want to see a clear baseline solution for linked list traversal.

Approach 2: Two-Pointer Method (O(n) time, O(1) space)

This is the optimal one-pass technique using the classic two pointers pattern. Maintain two pointers: slow and fast. Move fast two steps at a time and slow one step. When fast reaches the end of the list, slow will be at the middle node.

To delete the middle node, you also track the previous node of slow. Each iteration updates prev = slow, then advances the pointers (slow = slow.next, fast = fast.next.next). Once the loop ends, remove the middle by setting prev.next = slow.next. This eliminates the middle node without needing the list length.

The key insight is that the fast pointer moves twice as quickly, so when it finishes the list traversal, the slow pointer has covered exactly half the distance. This technique frequently appears in linked list tasks like cycle detection, finding the middle node, and splitting lists.

Recommended for interviews: Start by explaining the two-pass counting approach to demonstrate basic linked list traversal. Then move to the one-pass two-pointer technique, which most interviewers expect. Both run in O(n) time with O(1) space, but the single-pass solution shows stronger pointer manipulation skills.

Approach 1: Approach 1: Two-Pass Method

This approach involves traversing the list twice. In the first pass, you count the number of nodes to determine the middle node's index. In the second pass, you reach the middle node and update the links to skip it.

This C solution calculates the length of the list first and stores it in the count variable. Then it computes the middle index using count / 2. In the second pass, it traverses to the middle node and bypasses it by updating the next pointer of the previous node.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), where n is the number of nodes.
Space Complexity: O(1).

Try this approach in the editor →

Approach 2: Approach 2: Two-Pointer Method

This approach uses two pointers: a slow pointer and a fast pointer. The fast pointer moves twice as fast as the slow pointer. When the fast pointer reaches the end, the slow pointer will be at the middle node. We can then remove the middle node in a single pass.

This C implementation uses two pointers and a previous pointer to find the middle node and update the links, all in a single pass through the list.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n)
Space Complexity: O(1)

Try this approach in the editor →

Approach 3: Fast and Slow Pointers

The fast and slow pointer technique is a common method used to solve problems related to linked lists. We can maintain two pointers, a slow pointer slow and a fast pointer fast. Initially, slow points to a dummy node, whose next pointer points to the head node head of the list, while fast points to the head node head.

Then, we move the slow pointer one position backward and the fast pointer two positions backward each time, until the fast pointer reaches the end of the list. At this point, the node next to the node pointed by the slow pointer is the middle node of the list. We can remove the middle node by setting the next pointer of the node pointed by the slow pointer to point to the next next node.

The time complexity is O(n), where n is the length of the list. The space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Approach 1: Two-Pass Method

Time Complexity: O(n), where n is the number of nodes.
Space Complexity: O(1).

Approach 2: Two-Pointer Method

Time Complexity: O(n)
Space Complexity: O(1)

Fast and Slow Pointers

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Two-Pass Method (Count then Delete)O(n)O(1)When you want the simplest logic and explicit middle index
Two-Pointer Method (Fast & Slow)O(n)O(1)Preferred interview solution using a single traversal

Video Solution

Delete the Middle Node of a Linked List | Flipkart | Amazon | Microsoft | Leetcode 2095codestorywithMIK17,326 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Delete the Middle Node of a Linked List easy or hard?
The problem is classified as Medium difficulty on LeetCode. The logic is simple once you know the fast–slow pointer pattern, but candidates must correctly manage pointer updates and edge cases such as a single-node list.
Delete the Middle Node of a Linked List Python/Java solution
In Python or Java, maintain fast and slow pointers starting at the head and track the previous node of slow. Advance fast by two nodes and slow by one node until fast reaches the end. Then update prev.next to skip the slow node. The implementation runs in O(n) time and O(1) space.
How to solve Delete the Middle Node of a Linked List in O(n)?
Use the fast and slow pointer technique. Initialize slow, fast, and a previous pointer. Move fast two nodes at a time and slow one node at a time while tracking the previous node of slow. When fast reaches the end, delete the middle by setting prev.next to slow.next. The list is processed once, giving O(n) time and O(1) space.
What is the best approach for Delete the Middle Node of a Linked List?
The two-pointer (fast and slow pointer) approach is the best solution. It finds the middle node in a single traversal by moving the fast pointer two steps and the slow pointer one step at a time. When the fast pointer reaches the end, the slow pointer is at the middle. This method runs in O(n) time with O(1) extra space.
Is Delete the Middle Node of a Linked List asked at Google/Amazon/Meta?
Linked list pointer manipulation problems are common in interviews at companies like Amazon, Google, and Meta. Variants such as finding the middle node, removing the Nth node from the end, or deleting nodes during traversal frequently appear in technical screens.
What data structure is used in Delete the Middle Node of a Linked List?
The problem uses a singly linked list. Each node contains a value and a pointer to the next node. Solving it efficiently relies on pointer traversal techniques such as the fast–slow two-pointer method.
What is the time complexity of Delete the Middle Node of a Linked List?
The time complexity is O(n) because the algorithm must traverse the linked list to reach the middle. The two-pass method performs two linear scans, while the fast–slow pointer method completes the task in a single pass. Both approaches use O(1) additional space.

Ready to solve this problem?

Practice Delete the Middle Node of a Linked List with our built-in code editor and test cases.

Practice on FleetCode