Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA
  • Interview Problems on Linked List
  • Practice Linked List
  • MCQs on Linked List
  • Linked List Tutorial
  • Types of Linked List
  • Singly Linked List
  • Doubly Linked List
  • Circular Linked List
  • Circular Doubly Linked List
  • Linked List vs Array
  • Time & Space Complexity
  • Advantages & Disadvantages
Open In App
Next Article:
Delete Nth node from the end of the given linked list
Next article icon

Delete Nth node from the end of the given linked list

Last Updated : 03 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a linked list and an integer N, the task is to delete the Nth node from the end of the given linked list.

Examples:  

Input: 2 -> 3 -> 1 -> 7 -> NULL, N = 1 
Output: 2 3 1
Explanation: The created linked list is: 2 3 1 7 
The linked list after deletion is: 2 3 1

Input: 1 -> 2 -> 3 -> 4 -> NULL, N = 4 
Output: 2 3 4
Explanation: The created linked list is: 1 2 3 4 
The linked list after deletion is: 2 3 4 

Input: 5 -> 3 -> 8 -> 6 -> NULL, N = 2 
Output: 5 3 6
Explanation: The created linked list is: 5 3 8 6
The linked list after deletion is: 5 3 6 

Approach - Using Length of List - O(n) time and O(1) space

The idea is to first determine the length of the linked list (k) by traversing it once. Once the length is known, the node to be removed is the (k - n + 1)th node from the beginning. We then traverse the list again to reach the node just before the one to be deleted and adjust its next pointer to skip the target node, effectively removing it from the list. If the node to be removed is the head (i.e., k - n == 0), we simply return the second node as the new head.

C++
// C++ code for the deleting a node from end // in two traversal #include <bits/stdc++.h> using namespace std;  class Node { public:     int data;     Node* next;     Node(int x) {         this->data = x;         this->next = NULL;     } };  Node* deleteNthNodeFromEnd(Node* head, int n) {     int k = 0;     Node* curr = head;          // Find length of  list      while (curr != nullptr) {         curr = curr->next;         k++;     }          // if head is the nth node from end      if (k-n == 0) return head->next;          // Reach the node just before     // the target node.     curr = head;     for (int i=1; i<k-n; i++) {         curr = curr->next;     }      // Skip the target node     curr->next = curr->next->next;      return head; }  int main() {     Node* head = new Node(1);     head->next = new Node(2);     head->next->next = new Node(3);     head->next->next->next = new Node(4);     head->next->next->next->next = new Node(5);      head = deleteNthNodeFromEnd(head, 4);      Node* curr = head;     while (curr) {         cout << curr->data << " ";         curr = curr->next;     }     cout << endl;     return 0; } 
Java
// Java code for the deleting a node from end // in two traversal  class Node {     int data;     Node next;     Node(int x) {         this.data = x;         this.next = null;     } }  class GfG {     static Node deleteNthNodeFromEnd(Node head, int n) {         int k = 0;         Node curr = head;                  // Find length of  list          while (curr != null) {             curr = curr.next;             k++;         }                  // if head is the nth node from end          if (k - n == 0) return head.next;                  // Reach the node just before         // the target node.         curr = head;         for (int i = 1; i < k - n; i++) {             curr = curr.next;         }          // Skip the target node         curr.next = curr.next.next;          return head;     }      public static void main(String[] args) {         Node head = new Node(1);         head.next = new Node(2);         head.next.next = new Node(3);         head.next.next.next = new Node(4);         head.next.next.next.next = new Node(5);          head = deleteNthNodeFromEnd(head, 4);          Node curr = head;         while (curr != null) {             System.out.print(curr.data + " ");             curr = curr.next;         }         System.out.println();     } } 
Python
# Python code for the deleting a node from end # in two traversal  class Node:     def __init__(self, x):         self.data = x         self.next = None  def deleteNthNodeFromEnd(head, n):     k = 0     curr = head          # Find length of list      while curr:         curr = curr.next         k += 1          # if head is the nth node from end      if k - n == 0:         return head.next          # Reach the node just before     # the target node.     curr = head     for _ in range(1, k - n):         curr = curr.next      # Skip the target node     curr.next = curr.next.next          return head  if __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)      head = deleteNthNodeFromEnd(head, 4)      curr = head     while curr:         print(curr.data, end=" ")         curr = curr.next     print() 
C#
// C# code for the deleting a node from end // in two traversal using System;  class Node {     public int data;     public Node next;     public Node(int x) {         this.data = x;         this.next = null;     } }  class GfG {     static Node deleteNthNodeFromEnd(Node head, int n) {         int k = 0;         Node curr = head;                  // Find length of list          while (curr != null) {             curr = curr.next;             k++;         }                  // if head is the nth node from end          if (k - n == 0) return head.next;                  // Reach the node just before         // the target node.         curr = head;         for (int i = 1; i < k - n; i++) {             curr = curr.next;         }          // Skip the target node         curr.next = curr.next.next;          return head;     }      static void Main(string[] args) {         Node head = new Node(1);         head.next = new Node(2);         head.next.next = new Node(3);         head.next.next.next = new Node(4);         head.next.next.next.next = new Node(5);          head = deleteNthNodeFromEnd(head, 4);          Node curr = head;         while (curr != null) {             Console.Write(curr.data + " ");             curr = curr.next;         }         Console.WriteLine();     } } 
JavaScript
// JavaScript code for the deleting a node from end // in two traversal  class Node {     constructor(x) {         this.data = x;         this.next = null;     } }  function deleteNthNodeFromEnd(head, n) {     let k = 0;     let curr = head;          // Find length of list      while (curr) {         curr = curr.next;         k++;     }          // if head is the nth node from end      if (k - n === 0) return head.next;          // Reach the node just before     // the target node.     curr = head;     for (let i = 1; i < k - n; i++) {         curr = curr.next;     }      // Skip the target node     curr.next = curr.next.next;      return head; }  let head = new Node(1); head.next = new Node(2); head.next.next = new Node(3); head.next.next.next = new Node(4); head.next.next.next.next = new Node(5);  head = deleteNthNodeFromEnd(head, 4);  let curr = head; while (curr) {     process.stdout.write(curr.data + " ");     curr = curr.next; } console.log(); 

Output
1 3 4 5  

Approach - Using Fast and Slow Pointers - O(n) time and O(1) space

The idea is to use two pointers, fast and slow, to traverse the linked list. The fast pointer is moved n steps ahead first, and then both pointers are moved together until fast reaches the end. At this point, slow will be just before the node to be deleted, allowing us to remove the nth node from the end efficiently in a single pass.

C++
// C++ code for the deleting a node from end // in two traversal #include <bits/stdc++.h> using namespace std;  class Node { public:     int data;     Node* next;     Node(int x) {         this->data = x;         this->next = NULL;     } };  Node* deleteNthNodeFromEnd(Node* head, int n) {          Node* fast = head;     Node* slow = head;      // Move the fast pointer n nodes     for (int i = 0; i < n; i++)         fast = fast->next;      // If fast becomes NULL, then head      // is the nth node from end.     if (fast == nullptr)         return head->next;      // Move both pointers until fast reaches the end     while (fast->next != nullptr) {         fast = fast->next;         slow = slow->next;     }      // Remove the nth node from the end     slow->next = slow->next->next;     return head; }  int main() {     Node* head = new Node(1);     head->next = new Node(2);     head->next->next = new Node(3);     head->next->next->next = new Node(4);     head->next->next->next->next = new Node(5);      head = deleteNthNodeFromEnd(head, 4);      Node* curr = head;     while (curr) {         cout << curr->data << " ";         curr = curr->next;     }     cout << endl;     return 0; } 
Java
// Java code for the deleting a node from end // in two traversal import java.util.ArrayList;  class Node {     int data;     Node next;          Node(int x) {         this.data = x;         this.next = null;     } }  class GfG {     static Node deleteNthNodeFromEnd(Node head, int n) {         Node fast = head;         Node slow = head;          // Move the fast pointer n nodes         for (int i = 0; i < n; i++)             fast = fast.next;          // If fast becomes NULL, then head          // is the nth node from end.         if (fast == null)             return head.next;          // Move both pointers until fast reaches the end         while (fast.next != null) {             fast = fast.next;             slow = slow.next;         }          // Remove the nth node from the end         slow.next = slow.next.next;         return head;     }      public static void main(String[] args) {         Node head = new Node(1);         head.next = new Node(2);         head.next.next = new Node(3);         head.next.next.next = new Node(4);         head.next.next.next.next = new Node(5);          head = deleteNthNodeFromEnd(head, 4);          Node curr = head;         while (curr != null) {             System.out.print(curr.data + " ");             curr = curr.next;         }         System.out.println();     } } 
Python
# Python code for the deleting a node from end # in two traversal  class Node:     def __init__(self, x):         self.data = x         self.next = None  def deleteNthNodeFromEnd(head, n):     fast = head     slow = head      # Move the fast pointer n nodes     for _ in range(n):         fast = fast.next      # If fast becomes None, then head      # is the nth node from end.     if fast is None:         return head.next      # Move both pointers until fast reaches the end     while fast.next is not None:         fast = fast.next         slow = slow.next      # Remove the nth node from the end     slow.next = slow.next.next     return head  if __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)      head = deleteNthNodeFromEnd(head, 4)      curr = head     while curr:         print(curr.data, end=" ")         curr = curr.next     print() 
C#
// C# code for the deleting a node from end // in two traversal using System; using System.Collections.Generic;  class Node {     public int data;     public Node next;          public Node(int x) {         this.data = x;         this.next = null;     } }  class GfG {     static Node deleteNthNodeFromEnd(Node head, int n) {         Node fast = head;         Node slow = head;          // Move the fast pointer n nodes         for (int i = 0; i < n; i++)             fast = fast.next;          // If fast becomes NULL, then head          // is the nth node from end.         if (fast == null)             return head.next;          // Move both pointers until fast reaches the end         while (fast.next != null) {             fast = fast.next;             slow = slow.next;         }          // Remove the nth node from the end         slow.next = slow.next.next;         return head;     }      static void Main() {         Node head = new Node(1);         head.next = new Node(2);         head.next.next = new Node(3);         head.next.next.next = new Node(4);         head.next.next.next.next = new Node(5);          head = deleteNthNodeFromEnd(head, 4);          Node curr = head;         while (curr != null) {             Console.Write(curr.data + " ");             curr = curr.next;         }         Console.WriteLine();     } } 
JavaScript
// JavaScript code for the deleting a node from end // in two traversal  class Node {     constructor(x) {         this.data = x;         this.next = null;     } }  function deleteNthNodeFromEnd(head, n) {     let fast = head;     let slow = head;      // Move the fast pointer n nodes     for (let i = 0; i < n; i++)         fast = fast.next;      // If fast becomes null, then head      // is the nth node from end.     if (fast === null)         return head.next;      // Move both pointers until fast reaches the end     while (fast.next !== null) {         fast = fast.next;         slow = slow.next;     }      // Remove the nth node from the end     slow.next = slow.next.next;     return head; }  let head = new Node(1); head.next = new Node(2); head.next.next = new Node(3); head.next.next.next = new Node(4); head.next.next.next.next = new Node(5);  head = deleteNthNodeFromEnd(head, 4);  let curr = head; while (curr !== null) {     process.stdout.write(curr.data + " ");     curr = curr.next; } console.log(); 

Output
1 3 4 5  



Next Article
Delete Nth node from the end of the given linked list

G

gp6
Improve
Article Tags :
  • Misc
  • Linked List
  • DSA
  • Delete a Linked List
  • Traversal
Practice Tags :
  • Linked List
  • Misc
  • Traversal

Similar Reads

    Remove Nth node from end of the Linked List
    Given a linked list. The task is to remove the Nth node from the end of the linked list.Examples: Input : LinkedList = 1 ->2 ->3 ->4 ->5 , N = 2Output : 1 ->2 ->3 ->5Explanation: Linked list after deleting the 2nd node from last which is 4, is 1 ->2 ->3 ->5 Input : Link
    15+ min read
    Program for Nth node from the end of a Linked List
    Given a Linked List of M nodes and a number N, find the value at the Nth node from the end of the Linked List. If there is no Nth node from the end, print -1.Examples:Input: 1 -> 2 -> 3 -> 4, N = 3Output: 2Explanation: Node 2 is the third node from the end of the linked list.Input: 35 ->
    14 min read
    XOR Linked List - Find Nth Node from the end
    Given a XOR linked list and an integer N, the task is to print the Nth node from the end of the given XOR linked list. Examples: Input: 4 –> 6 –> 7 –> 3, N = 1 Output: 3 Explanation: 1st node from the end is 3.Input: 5 –> 8 –> 9, N = 4 Output: Wrong Input Explanation: The given Xor Li
    15+ min read
    Move first element to end of a given Linked List
    Write a C function that moves first element to end in a given Singly Linked List. For example, if the given Linked List is 1->2->3->4->5, then the function should change the list to 2->3->4->5->1. Algorithm: Traverse the list till last node. Use two pointers: one to store the
    13 min read
    Delete Kth nodes from the beginning and end of a Linked List
    Given a singly Linked List and an integer K denoting the position of a Linked List, the task is to delete the Kth node from the beginning and end of the Linked List. Examples: Input: 1 ? 2 ? 3 ? 4 ? 5 ? 6, K = 3Output: 1 ? 2 ? 5 ? 6Explanation: Deleted Nodes : 3, 4 Input: 1 ? 2 ? 3 ? 4 ? 5 ? 6, K =
    13 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences