# Delete Nth Node of the Singly Linked List

“** Delete Nth Node of the Singly Linked List**” is a basic problem exercise for beginners based on linked list. Here, we are given a linked list and our task to delete the node having with position ‘n’.

To do so, we need to traverse the linked list to node just before the nth node, then change the pointer of that node to next to next.

For example, we need to delete the 2^{nd} node from the following linked list.

The steps required to delete the nth node from the linked list are as follows:

- If Linked List is empty, return.
- If the Node to be deleted is first node, then make head to the second node, if exist and delete the first node. If second node does not exist, then mark head of the Linked List to NULL.
- Traverse to nth node of the linked list and mark pointer to just before the nth node know as ‘previous’ node.
- If at any case there is less number of ‘n’ nodes in the linked list, return.
- Else, point the pointer of previous node to the next of nth node.
- Delete nth node of the linked list.

**C++ Program to delete Nth node of the linked list is as follows:**

#include<bits/stdc++.h> using namespace std; /* Structure of node of the linked list */ struct node { int data; struct node *next; }; /* Function to Insert Node at the end of the Linked List */ void insert_end(struct node **head, int ele) { /* Creating a new node */ struct node *new_node; new_node = (struct node*)malloc(sizeof(struct node)); new_node->data = ele; new_node->next = NULL; /* Check whether the linked list is empty or not */ /* If Linked List is empty */ if(*head == NULL) { *head = new_node; return; } /* If Linked List is not empty, then traverse the linked list and point the pointer of the last node to the new node*/ struct node *temp = NULL; temp = (*head); while(temp->next != NULL) temp=temp->next; temp->next = new_node; } /* Function to delete Nth Node of the Linked List */ void deleteNth(struct node **head,int n) { /* If Linked List is empty, there is no node to delete, hence return */ if( *head == NULL) return; struct node *temp = *head , *prev = NULL; /* If first node is to be deleted, then mark the head to the next of first node, and delete first node*/ if(n==1) { (*head) = (*head) -> next; delete temp; return; } int i; /* For Nth Node to be deleted, traverse the linked list to Nth node and store the pointer to (n-1)th node, after that mark the pointer of (n-1)th node to the next of nth node. Then, delete Nth Node */ for(i = 1; i < n && temp!=NULL; i++) { prev = temp; temp=temp->next; } if(i < n) return; // there are less number of nodes prev->next = temp->next; delete temp; } /* Function to print Nth Node*/ void print(struct node *head) { struct node *p = head; while(p!=NULL) { cout<<p->data<<" "; p=p->next; } } int main() { struct node *head; head = NULL; /* Inserting Some Nodes in the Linked List */ insert_end(&head,1); insert_end(&head,2); insert_end(&head,3); insert_end(&head,4); cout<<"The linked list elements before deleting 3rd node: \n"; print(head); deleteNth(&head,3); cout<<"\n The linked list elements after deleting 3rd node: \n"; print(head); }

The linked list elements before deleting 3rd node: 1 2 3 4 The linked list elements after deleting 3rd node: 1 2 4OUTPUT:

**Related Posts:**

**Delete Complete Linked List.****Delete without head pointer of the Linked List.****Delete All Occurrences of particular node of the Linked List.****Delete Alternate Nodes of the Linked List.****Delete every ‘N’ Nodes After ‘M’ Nodes of the Linked List.****Remove Duplicate Nodes from an Unsorted Linked List.****Remove Duplicate Nodes from a Sorted Linked List.****Find Union and Intersection of Two Linked List.****Merge Two Sorted Linked List.****Insert a New Node at the Sorted Linked List.****Reverse a Linked List.****Reverse a Linked List Using Stack.****Printing Linked List in Reverse Order without actually reversing the Linked List.****Swap Adjacent Elements of the Linked List.****Count All Occurrences of a Particular Node in a Linked List.****Bubble Sort on Linked List.****Detect a Loop in a Linked List.****Find the Length of the Loop present in the Linked List.****Detect and Remove Loop from a Linked List.****Segregate Even and Odd Nodes of the Linked List.**