# Delete middle of linked list in C++ program

C++Server Side ProgrammingProgramming

In this tutorial, we are going to learn how to delete the middle node in a linked list.

The solution to the problem is straightforward. We will have two pointers one moves one node at a time and the other one moves two nodes at a time. By the time the second pointer reaches the final node, the first will be in the middle of the linked list.

Let's see the steps to solve the problem.

• Write a struct Node for the linked list node.

• Initialize the linked list with the dummy data.

• Write a function to delete the linked list.

• Iterate over the linked list until the fast pointer reaches the end.

• Move the slow pointer to one next node.

• Move the fast pointer to the next node of the next node.

## Example

Let's see the code.

Live Demo

#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
struct Node* deleteMiddleNode(struct Node* head) {
return NULL;
}
return NULL;
}
struct Node* prev;
while (fast_ptr != NULL && fast_ptr->next != NULL) {
fast_ptr = fast_ptr->next->next;
prev = slow_ptr;
slow_ptr = slow_ptr->next;
}
prev->next = slow_ptr->next;
delete slow_ptr;
}
while (node != NULL) {
cout << node->data << " -> ";
node = node->next;
}
cout << "Null" << endl;
}
Node* newNode(int data) {
struct Node* temp = new Node;
temp->data = data;
temp->next = NULL;
return temp;
}
int main() {
cout << "Linked list before deleting middle node: ";
cout << "Linked List after deleting middle node: ";
return 0;
}

## Output

If you execute the above program, then you will get the following result.

Linked list before deleting middle node: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> Null
Linked List after deleting middle node: 1 -> 2 -> 3 -> 5 -> 6 -> Null

## Conclusion

If you have any queries in the tutorial, mention them in the comment section.