Open In App

Delete middle of linked list

Last Updated : 14 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

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:

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

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

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

The basic idea behind this approach is 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,

Below is the implementation of the above approach:

C++
// C++ program to delete middle of a linked list
#include <bits/stdc++.h>
using namespace std;

struct Node {
    int data;
    Node* next;

    Node(int x){
        data = x;
        next = nullptr;
    }
};

// Function to delete middle node from linked list.
Node* deleteMid(Node* head) {
    // Edge case: return nullptr if there is only
    // one node.
    if (head->next == nullptr)
        return nullptr;

    int count = 0;
    Node *p1 = head, *p2 = head;

    // First pass, count the number of nodes
    // in the linked list using 'p1'.
    while (p1 != nullptr) {
        count++;
        p1 = p1->next;
    }

    // Get the index of the node to be deleted.
    int middleIndex = count / 2;

    // Second pass, let 'p2' move toward the predecessor
    // of the middle node.
    for (int i = 0; i < middleIndex - 1; ++i)
        p2 = p2->next;

    // Delete the middle node and return 'head'.
    p2->next = p2->next->next;
    return head;
}

void printList(Node* head) {
    Node* temp = head;
    while (temp != nullptr) {
        cout << temp->data << " -> ";
        temp = temp->next;
    }
    cout << "nullptr" << endl;
}

int main() {
    // Create a static hardcoded linked list:
    // 1 -> 2 -> 3 -> 4 -> 5.
    Node* head = new Node(1);
    head->next = new Node(2);
    head->next->next = new Node(3);
    head->next->next->next = new Node(4);
    head->next->next->next->next = new Node(5);

    cout << "Original Linked List: ";
    printList(head);

    // Delete the middle node.
    head = deleteMid(head);

    cout << "Linked List after deleting the middle node: ";
    printList(head);

    return 0;
}
C Java Python C# JavaScript

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.

Below is the implementation of the above approach

C++
// C++ program to delete middle of a linked list
#include <bits/stdc++.h>
using namespace std;

struct Node {
    int data;
    Node* next;

    Node(int x){
        data = x;
        next = nullptr;
    }
};

// Function to delete middle node from linked list
struct Node* deleteMid(struct Node* head) {
    // If the list is empty, return NULL
    if (head == NULL)
        return NULL;

    // If the list has only one node,
    // delete it and return NULL
    if (head->next == NULL) {
        delete head;
        return NULL;
    }

    struct Node* prev = NULL;
    struct Node* slow_ptr = head;
    struct Node* fast_ptr = head;

    // Move the fast pointer 2 nodes ahead
    // and the slow pointer 1 node ahead
    // until fast pointer reaches end of the list
    while (fast_ptr != NULL && fast_ptr->next != NULL) {
        fast_ptr = fast_ptr->next->next;  

        // Update prev to hold the previous 
        // slow pointer value
        prev = slow_ptr;  

        slow_ptr = slow_ptr->next;  
    }

    // At this point, slow_ptr points to middle node
    // Bypass the middle node
    prev->next = slow_ptr->next;

    // Delete the middle node
    delete slow_ptr; 

    // Return the head of the modified list
    return head;
}

void printList(struct Node* head) {
    struct Node* temp = head;
    while (temp != NULL) {
        cout << temp->data << " -> ";
        temp = temp->next;
    }
    cout << "NULL" << endl;
}

int main() {
    // Create a static hardcoded linked list:
    // 1 -> 2 -> 3 -> 4 -> 5
    Node* head = new Node(1);
    head->next = new Node(2);
    head->next->next = new Node(3);
    head->next->next->next = new Node(4);
    head->next->next->next->next = new Node(5);

    cout << "Original Linked List: ";
    printList(head);

    // Delete the middle node
    head = deleteMid(head);

    cout << "Linked List after deleting the middle node: ";
    printList(head);

    return 0;
}
C Java Python C# JavaScript

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:



Next Article
Article Tags :
Practice Tags :

Similar Reads

three90RightbarBannerImg