Remove Nth node from end of the Linked List | CPP program to delete nth node from last ; Structure of node ; Function to insert node in a linked list ; Function to remove nth node from last ; To store length of the linked list ; B > length , then we can 't remove node ; We need to remove head node ; Return head -> next ; Remove len - B th node from starting ; This function prints contents of linked list starting from the given node ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * next ; } ; struct Node * create ( struct Node * head , int x ) { struct Node * temp , * ptr = head ; temp = new Node ( ) ; temp -> data = x ; temp -> next = NULL ; if ( head == NULL ) head = temp ; else { while ( ptr -> next != NULL ) { ptr = ptr -> next ; } ptr -> next = temp ; } return head ; } Node * removeNthFromEnd ( Node * head , int B ) { int len = 0 ; Node * tmp = head ; while ( tmp != NULL ) { len ++ ; tmp = tmp -> next ; } if ( B > len ) { cout << " Length ▁ of ▁ the ▁ linked ▁ list ▁ is ▁ " << len ; cout << " ▁ we ▁ can ' t ▁ remove ▁ " << B << " th ▁ node ▁ from ▁ the " ; cout << " ▁ linked ▁ list STRNEWLINE " ; return head ; } else if ( B == len ) { return head -> next ; } else { int diff = len - B ; Node * prev = NULL ; Node * curr = head ; for ( int i = 0 ; i < diff ; i ++ ) { prev = curr ; curr = curr -> next ; } prev -> next = curr -> next ; return head ; } } void display ( struct Node * head ) { struct Node * temp = head ; while ( temp != NULL ) { cout << temp -> data << " ▁ " ; temp = temp -> next ; } cout << endl ; } int main ( ) { struct Node * head = NULL ; head = create ( head , 1 ) ; head = create ( head , 2 ) ; head = create ( head , 3 ) ; head = create ( head , 4 ) ; head = create ( head , 5 ) ; int n = 2 ; cout << " Linked ▁ list ▁ before ▁ modification : ▁ STRNEWLINE " ; display ( head ) ; head = removeNthFromEnd ( head , 2 ) ; cout << " Linked ▁ list ▁ after ▁ modification : ▁ STRNEWLINE " ; display ( head ) ; return 0 ; }