Delete Nth node from the end of the given linked list
Last Updated : 03 Mar, 2025
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();
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();
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