VOOZH about

URL: https://www.geeksforgeeks.org/dsa/delete-middle-of-linked-list/

⇱ Delete middle of linked list - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Delete middle of linked list

Last Updated : 3 Apr, 2026

Given a singly linked list, the task is to delete the middle node of the list.

  • If the list contains an even number of nodes, there will be two middle nodes. In this case, delete the second middle node.
  • If the linked list consists of only one node, then return NULL.

Example:

Input: LinkedList: 1->2->3->4->5
Output: 1->2->4->5
Explanation:

👁 Image

Input: LinkedList: 2->4->6->7->5->1
Output: 2->4->6->5->1
Explaination:

👁 Image

Input: LinkedList: 7
Output: <empty linked list>

[Naive Approach] Using Two-Pass Traversal - O(n) Time and O(1) space

The basic to first traverse the entire linked list to count the total number of nodes. Once we know the total number of nodes, we can calculate the position of the middle node, which is at index n/2 (where n is the total number of nodes). Then go through the linked list again, but this time we stop right before the middle node. Once there, we modify the next pointer of the node before the middle node so that it skips over the middle node and points directly to the node after it,


Output
Original Linked List: 1 -> 2 -> 3 -> 4 -> 5 -> nullptr
Linked List after deleting the middle node: 1 -> 2 -> 4 -> 5 -> nullptr

Time Complexity: O(n). Two traversals of the linked list are needed
Auxiliary Space: O(1). No extra space is needed.

[Expected Approach] One-Pass Traversal with Slow and Fast Pointers - O(n) Time and O(1) Space

The above solution requires two traversals of the linked list. The middle node can be deleted using one traversal. The idea is to use two pointers, slow_ptr, and fast_ptr. The fast pointer moves two nodes at a time, while the slow pointer moves one node at a time. When the fast pointer reaches the end of the list, the slow pointer will be positioned at the middle node. Next, you need to connect the node that comes before the middle node (prev) to the node that comes after the middle node. This effectively skips over the middle node, removing it from the list.


Output
Original Linked List: 1 -> 2 -> 3 -> 4 -> 5 -> NULL
Linked List after deleting the middle node: 1 -> 2 -> 4 -> 5 -> NULL

Time Complexity: O(n). Only one traversal of the linked list is needed
Auxiliary Space: O(1). As no extra space is needed.

Related Article:

Comment
Article Tags:
Article Tags: