Delete middle of linked list in C++?


Let us first define our linked list that contains data and the pointer to the next node.

struct Node {
   int data;
   struct Node* next;
};

Next we create our createNode(int data) function that takes int data as parameter and returns the newly created node after assigning the parameter value. The next pointer to the node will be null.

Node* createNode(int data){
   struct Node* newNode = new Node;
   newNode->data = data;
   newNode->next = NULL;
   return newNode;
}

Now we have our deleteMiddle(struct Node* head) function which takes the root node. If the root node isn’t null then it simply assigns the next value of the node previous to middle value to the node next to the middle value and returns the temphead which is the modified head.

struct Node* deleteMiddle(struct Node* head){
   if (head == NULL)
      return NULL;
   if (head->next == NULL) {
      delete head;
      return NULL;
   }
   Node* temphead = head;
   int count = nodeCount(head);
   int mid = count / 2;
   while (mid-- > 1) {
      head = head->next;
   }
   head->next = head->next->next;
   return temphead;
}

Finally we have our printList(Node *ptr) function which takes the list head and prints the list.

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

Example

Let us see the following implementation of deleting the middle of a singly linked list.

 Live Demo

#include <iostream>
using namespace std;
struct Node {
   int data;
   struct Node* next;
};
Node* createNode(int data){
   struct Node* newNode = new Node;
   newNode->data = data;
   newNode->next = NULL;
   return newNode;
}
int nodeCount(struct Node* head){
   int count = 0;
   while (head != NULL) {
      head = head->next;
      count++;
   }
   return count;
}
struct Node* deleteMiddle(struct Node* head){
   if (head == NULL)
      return NULL;
   if (head->next == NULL) {
      delete head;
      return NULL;
   }
   Node* temphead = head;
   int count = nodeCount(head);
   int mid = count / 2;
   while (mid-- > 1) {
      head = head->next;
   }
   head->next = head->next->next;
   return temphead;
}
void printList(Node * ptr){
   while (ptr!= NULL) {
      cout << ptr->data << "->";
      ptr = ptr->next;
   }
   cout << "NULL"<<endl;
}
int main(){
   struct Node* head = createNode(2);
   head->next = createNode(4);
   head->next->next = createNode(6);
   head->next->next->next = createNode(8);
   head->next->next->next->next = createNode(10);
   cout << "Original linked list"<<endl;
   printList(head);
   head = deleteMiddle(head);
   cout<<endl;
   cout << "After deleting the middle of the linked list"<<endl;
   printList(head);
   return 0;
}

Output

The above code will produce the following output −

Original linked list
2->4->6->8->10->NULL

After deleting the middle of the linked list
2->4->8->10->NULL

Updated on: 16-Jan-2021

305 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements