Delete Nth node from the end of the given linked list
Last Updated : 14 Jun, 2026
Given a linked list and an integer n, delete the nth node from the end of the given linked list.
Examples:
Input : LinkedList = 1 ->2 ->3 ->4 ->5 , n = 2 Output : 1 ->2 ->3 ->5 Explanation: Linked list after deleting the 2nd node from last which is 4, is 1 ->2 ->3 ->5
Input : LinkedList = 7 ->8 ->4 ->3 ->2 , n = 1 Output : 7 ->8 ->4 ->3 Explanation: Linked list after deleting the 1st node from last which is 2, is 7 ->8 ->4 ->3
[Naive Approach] Using Deletion from Front Approach - O(n) Time and O(1) Space
To remove the nth node from the end, first calculate the length of the linked list. Then, convert the problem into deleting the (length - n + 1)th node from the beginning of the list.
Traverse the linked list to calculate its length.
Compute the position of the node to delete from the front: nodeToDelete = (length - n + 1)
If nodeToDelete is 1, move the head to the next node.
Traverse the list till the (nodeToDelete - 1)th node.
Update its next pointer to skip the target node.
Return the modified linked list.
C++
#include<iostream>usingnamespacestd;classNode{public:intdata;Node*next;Node(intx){this->data=x;this->next=nullptr;}};Node*deleteNthNodeFromEnd(Node*head,intn){if(head==nullptr)returnhead;intk=0;Node*curr=head;// Find length of listwhile(curr!=nullptr){curr=curr->next;k++;}// If n is greater than length, do nothingif(n>k)returnhead;// If head is the nth node from endif(k-n==0){Node*temp=head;head=head->next;deletetemp;returnhead;}// Reach the node just before targetcurr=head;for(inti=1;i<k-n;i++){curr=curr->next;}// Delete the target nodeNode*temp=curr->next;curr->next=temp->next;deletetemp;returnhead;}voidprintList(Node*node){Node*curr=node;while(curr!=nullptr){cout<<curr->data<<" ";curr=curr->next;}}intmain(){Node*head=newNode(1);head->next=newNode(2);head->next->next=newNode(3);head->next->next->next=newNode(4);head->next->next->next->next=newNode(5);head=deleteNthNodeFromEnd(head,2);printList(head);return0;}
Java
importjava.util.*;// Node classclassNode{intdata;Nodenext;Node(intx){this.data=x;this.next=null;}}publicclassGFG{// Function to delete Nth node from endstaticNodedeleteNthNodeFromEnd(Nodehead,intn){if(head==null){returnhead;}intk=0;Nodecurr=head;// Find length of linked listwhile(curr!=null){curr=curr.next;k++;}// If n is greater than lengthif(n>k){returnhead;}// If head node needs to be deletedif(k-n==0){head=head.next;returnhead;}// Reach node just before target nodecurr=head;for(inti=1;i<k-n;i++){curr=curr.next;}// Delete target nodecurr.next=curr.next.next;returnhead;}// Function to print linked liststaticvoidprintList(Nodehead){Nodecurr=head;while(curr!=null){System.out.print(curr.data+" ");curr=curr.next;}}// Driver Codepublicstaticvoidmain(String[]args){Nodehead=newNode(1);head.next=newNode(2);head.next.next=newNode(3);head.next.next.next=newNode(4);head.next.next.next.next=newNode(5);// Function callhead=deleteNthNodeFromEnd(head,2);// Print updated linked listprintList(head);}}
Python
# Node classclassNode:def__init__(self,x):self.data=xself.next=None# Function to delete Nth node from enddefdeleteNthNodeFromEnd(head,n):ifheadisNone:returnheadk=0curr=head# Find length of linked listwhilecurrisnotNone:curr=curr.nextk+=1# If n is greater than lengthifn>k:returnhead# If head node needs to be deletedifk-n==0:head=head.nextreturnhead# Reach node just before target nodecurr=headforiinrange(1,k-n):curr=curr.next# Delete target nodecurr.next=curr.next.nextreturnhead# Function to print linked listdefprintList(head):curr=headwhilecurrisnotNone:print(curr.data,end=" ")curr=curr.next# Driver Codeif__name__=="__main__":head=Node(1)head.next=Node(2)head.next.next=Node(3)head.next.next.next=Node(4)head.next.next.next.next=Node(5)# Function callhead=deleteNthNodeFromEnd(head,2)# Print updated linked listprintList(head)
C#
usingSystem;// Node classclassNode{publicintdata;publicNodenext;publicNode(intx){this.data=x;this.next=null;}}classGFG{// Function to delete Nth node from endstaticNodedeleteNthNodeFromEnd(Nodehead,intn){if(head==null){returnhead;}intk=0;Nodecurr=head;// Find length of linked listwhile(curr!=null){curr=curr.next;k++;}// If n is greater than lengthif(n>k){returnhead;}// If head node needs to be deletedif(k-n==0){head=head.next;returnhead;}// Reach node just before target nodecurr=head;for(inti=1;i<k-n;i++){curr=curr.next;}// Delete target nodecurr.next=curr.next.next;returnhead;}// Function to print linked liststaticvoidprintList(Nodehead){Nodecurr=head;while(curr!=null){Console.Write(curr.data+" ");curr=curr.next;}}// Driver CodestaticvoidMain(){Nodehead=newNode(1);head.next=newNode(2);head.next.next=newNode(3);head.next.next.next=newNode(4);head.next.next.next.next=newNode(5);// Function callhead=deleteNthNodeFromEnd(head,2);// Print updated linked listprintList(head);}}
JavaScript
// Node classclassNode{constructor(x){this.data=x;this.next=null;}}// Function to delete Nth node from endfunctiondeleteNthNodeFromEnd(head,n){if(head===null){returnhead;}letk=0;letcurr=head;// Find length of linked listwhile(curr!==null){curr=curr.next;k++;}// If n is greater than lengthif(n>k){returnhead;}// If head node needs to be deletedif(k-n===0){head=head.next;returnhead;}// Reach node just before target nodecurr=head;for(leti=1;i<k-n;i++){curr=curr.next;}// Delete target nodecurr.next=curr.next.next;returnhead;}// Function to print linked listfunctionprintList(head){letcurr=head;while(curr!==null){process.stdout.write(curr.data+" ");curr=curr.next;}}// Driver Codelethead=newNode(1);head.next=newNode(2);head.next.next=newNode(3);head.next.next.next=newNode(4);head.next.next.next.next=newNode(5);// Function callhead=deleteNthNodeFromEnd(head,2);// Print updated linked listprintList(head);
Output
1 2 3 5
[Expected Approach] Using Fast and Slow Pointers - O(n) Time and O(1) Space
The idea is to first move the fast pointer n steps ahead, then move both fast and slow pointers together until fast reaches the end. The slow pointer will then be just before the node to be removed, allowing to update the next pointer to skip the target node.
Create a dummy node and connect it before the head node to handle edge cases easily.
Initialize two pointers, fast and slow, at the dummy node.
Move the fast pointer n + 1 steps ahead to maintain a gap of n nodes.
Move both fast and slow pointers together until fast reaches nullptr.
The slow pointer now points to the node just before the target node, so update its next pointer to skip the target node.
Delete the target node and return the updated linked list starting from dummy->next.
importjava.util.*;// Node classclassNode{intdata;Nodenext;Node(intx){this.data=x;this.next=null;}}publicclassGFG{// Function to delete Nth node from endstaticNodedeleteNthNodeFromEnd(Nodehead,intn){// Create dummy nodeNodedummy=newNode(0);dummy.next=head;// Initialize slow and fast pointersNodeslow=dummy;Nodefast=dummy;// Move fast pointer n+1 steps aheadfor(inti=0;i<=n;i++){// If n is greater than lengthif(fast==null){returnhead;}fast=fast.next;}// Move both pointers togetherwhile(fast!=null){fast=fast.next;slow=slow.next;}// Node to be deletedNodenodeDeleted=slow.next;// Delete target nodeif(nodeDeleted!=null){slow.next=nodeDeleted.next;}// Return updated headreturndummy.next;}// Function to print linked liststaticvoidprintList(Nodehead){Nodecurr=head;while(curr!=null){System.out.print(curr.data+" ");curr=curr.next;}}// Driver Codepublicstaticvoidmain(String[]args){Nodehead=newNode(1);head.next=newNode(2);head.next.next=newNode(3);head.next.next.next=newNode(4);head.next.next.next.next=newNode(5);// Function callhead=deleteNthNodeFromEnd(head,2);// Print updated linked listprintList(head);}}
Python
# Node classclassNode:def__init__(self,x):self.data=xself.next=None# Function to delete Nth node from enddefdeleteNthNodeFromEnd(head,n):# Create dummy nodedummy=Node(0)dummy.next=head# Initialize slow and fast pointersslow=dummyfast=dummy# Move fast pointer n+1 steps aheadforiinrange(n+1):# If n is greater than lengthiffastisNone:returnheadfast=fast.next# Move both pointers togetherwhilefastisnotNone:fast=fast.nextslow=slow.next# Node to be deletednodeDeleted=slow.next# Delete target nodeifnodeDeletedisnotNone:slow.next=nodeDeleted.next# Return updated headreturndummy.next# Function to print linked listdefprintList(head):curr=headwhilecurrisnotNone:print(curr.data,end=" ")curr=curr.next# Driver Codeif__name__=="__main__":head=Node(1)head.next=Node(2)head.next.next=Node(3)head.next.next.next=Node(4)head.next.next.next.next=Node(5)# Function callhead=deleteNthNodeFromEnd(head,2)# Print updated linked listprintList(head)
C#
usingSystem;// Node classclassNode{publicintdata;publicNodenext;publicNode(intx){this.data=x;this.next=null;}}classGFG{// Function to delete Nth node from endstaticNodedeleteNthNodeFromEnd(Nodehead,intn){// Create dummy nodeNodedummy=newNode(0);dummy.next=head;// Initialize slow and fast pointersNodeslow=dummy;Nodefast=dummy;// Move fast pointer n+1 steps aheadfor(inti=0;i<=n;i++){// If n is greater than lengthif(fast==null){returnhead;}fast=fast.next;}// Move both pointers togetherwhile(fast!=null){fast=fast.next;slow=slow.next;}// Node to be deletedNodenodeDeleted=slow.next;// Delete target nodeif(nodeDeleted!=null){slow.next=nodeDeleted.next;}// Return updated headreturndummy.next;}// Function to print linked liststaticvoidprintList(Nodehead){Nodecurr=head;while(curr!=null){Console.Write(curr.data+" ");curr=curr.next;}}// Driver CodestaticvoidMain(){Nodehead=newNode(1);head.next=newNode(2);head.next.next=newNode(3);head.next.next.next=newNode(4);head.next.next.next.next=newNode(5);// Function callhead=deleteNthNodeFromEnd(head,2);// Print updated linked listprintList(head);}}
JavaScript
// Node classclassNode{constructor(x){this.data=x;this.next=null;}}// Function to delete Nth node from endfunctiondeleteNthNodeFromEnd(head,n){// Create dummy nodeletdummy=newNode(0);dummy.next=head;// Initialize slow and fast pointersletslow=dummy;letfast=dummy;// Move fast pointer n+1 steps aheadfor(leti=0;i<=n;i++){// If n is greater than lengthif(fast===null){returnhead;}fast=fast.next;}// Move both pointers togetherwhile(fast!==null){fast=fast.next;slow=slow.next;}// Node to be deletedletnodeDeleted=slow.next;// Delete target nodeif(nodeDeleted!==null){slow.next=nodeDeleted.next;}// Return updated headreturndummy.next;}// Function to print linked listfunctionprintList(head){letcurr=head;while(curr!==null){process.stdout.write(curr.data+" ");curr=curr.next;}}// Driver Codelethead=newNode(1);head.next=newNode(2);head.next.next=newNode(3);head.next.next.next=newNode(4);head.next.next.next.next=newNode(5);// Function callhead=deleteNthNodeFromEnd(head,2);// Print updated linked listprintList(head);