Skip to main content

Deleting middle node of linked list.

KANISHKA GUPTA
EditReport

Delete the Middle Node of a Linked List

Problem Description

Given a singly linked list, the task is to delete the middle node. If the list has an even number of nodes, delete the second middle node. For example, given the linked list 1 -> 2 -> 3 -> 4 -> 5, after deletion, it should become 1 -> 2 -> 4 -> 5. If the list is 1 -> 2 -> 3 -> 4, it should become 1 -> 2 -> 4.

Video Explanation

Approach

To delete the middle node of a linked list, we can use the following approach:

  1. Find the Length of the List:

    • Traverse the list to count the total number of nodes.
  2. Determine the Middle Index:

    • Calculate the middle index based on whether the length is even or odd.
  3. Traverse to the Node Before the Middle:

    • Move through the list until reaching the node just before the middle node.
  4. Delete the Middle Node:

    • Adjust the pointers to bypass the middle node, effectively removing it from the list.

Implementation

Solutions

#include <iostream>

class Node {
public:
int value;
Node* next;

Node(int val) {
value = val;
next = nullptr;
}
};

class LinkedList {
public:
Node* head;

LinkedList() {
head = nullptr;
}

// Function to delete the middle node
void deleteMiddle() {
if (head == nullptr) return; // If the list is empty

// Step 1: Find the length of the list
int length = 0;
Node* current = head;
while (current != nullptr) {
length++;
current = current->next;
}

// Step 2: Determine the middle index
int middleIndex = length / 2;

// Step 3: Traverse to the node before the middle
current = head;
for (int i = 0; i < middleIndex - 1; i++) {
current = current->next;
}

// Step 4: Delete the middle node
if (current->next != nullptr) {
current->next = current->next->next; // Bypass the middle node
} else {
// If the list has only one node
head = nullptr;
}
}

// Function to add a new node at the end of the list
void append(int value) {
Node* newNode = new Node(value);
if (head == nullptr) {
head = newNode;
return;
}

Node* current = head;
while (current->next != nullptr) {
current = current->next;
}
current->next = newNode;
}

// Function to print the linked list
void printList() {
Node* current = head;
while (current != nullptr) {
std::cout << current->value << " -> ";
current = current->next;
}
std::cout << "nullptr" << std::endl;
}
};

// Example usage
int main() {
LinkedList ll;
ll.append(1);
ll.append(2);
ll.append(3);
ll.append(4);
ll.append(5);

std::cout << "Original Linked List: ";
ll.printList();

ll.deleteMiddle();

std::cout << "Linked List After Deleting Middle Node: ";
ll.printList();

return 0;
}
Track Your Progress

Done with this topic? Mark it as complete to track your progress.

💬 Discuss this page

Have a question or spot something confusing in "Deleting middle node of linked list."? Ask below — it's backed by GitHub Discussions, so maintainers get notified like any other GitHub activity.