Delete middle of linked list | C ++ program to delete middle of a linked list ; Link list Node ; count of nodes ; Deletes middle node and returns head of the modified list ; Base cases ; Find the count of nodes ; Find the middle node ; Delete the middle node ; Delete the middle node ; A utility function to print a given linked list ; Utility function to create a new node . ; Driver program to test above function ; Start with the empty list
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * next ; } ; int countOfNodes ( struct Node * head ) { int count = 0 ; while ( head != NULL ) { head = head -> next ; count ++ ; } return count ; } struct Node * deleteMid ( struct Node * head ) { if ( head == NULL ) return NULL ; if ( head -> next == NULL ) { delete head ; return NULL ; } struct Node * copyHead = head ; int count = countOfNodes ( head ) ; int mid = count / 2 ; while ( mid -- > 1 ) { head = head -> next ; } head -> next = head -> next -> next ; return copyHead ; } void printList ( struct Node * ptr ) { while ( ptr != NULL ) { cout << ptr -> data << " - > " ; ptr = ptr -> next ; } cout << " NULL STRNEWLINE " ; } Node * newNode ( int data ) { struct Node * temp = new Node ; temp -> data = data ; temp -> next = NULL ; return temp ; } int main ( ) { struct Node * head = newNode ( 1 ) ; head -> next = newNode ( 2 ) ; head -> next -> next = newNode ( 3 ) ; head -> next -> next -> next = newNode ( 4 ) ; cout << " Given ▁ Linked ▁ List STRNEWLINE " ; printList ( head ) ; head = deleteMid ( head ) ; cout << " Linked ▁ List ▁ after ▁ deletion ▁ of ▁ middle STRNEWLINE " ; printList ( head ) ; return 0 ; }