Delete Nth node from the end of the given linked list (original) (raw)

Last Updated : 14 Jun, 2026

Given a linked list and an integer **n, delete the **n th 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

frame_2

**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

frame_1

Table of Content

[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.

#include using namespace std;

class Node { public: int data; Node *next; Node(int x) { this->data = x; this->next = nullptr; } };

Node *deleteNthNodeFromEnd(Node *head, int n) { if (head == nullptr) return head;

int k = 0;
Node *curr = head;

// Find length of list
while (curr != nullptr)
{
    curr = curr->next;
    k++;
}

// If n is greater than length, do nothing
if (n > k)
    return head;

// If head is the nth node from end
if (k - n == 0)
{
    Node *temp = head;
    head = head->next;
    delete temp;
    return head;
}

// Reach the node just before target
curr = head;
for (int i = 1; i < k - n; i++)
{
    curr = curr->next;
}

// Delete the target node
Node *temp = curr->next;
curr->next = temp->next;
delete temp;

return head;

}

void printList(Node *node) { Node *curr = node; while (curr != nullptr) { cout << curr->data << " "; curr = curr->next; } }

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, 2);

printList(head);

return 0;

}

Java

import java.util.*;

// Node class class Node { int data; Node next;

Node(int x)
{
    this.data = x;
    this.next = null;
}

}

public class GFG { // Function to delete Nth node from end static Node deleteNthNodeFromEnd(Node head, int n) { if (head == null) { return head; }

    int k = 0;

    Node curr = head;

    // Find length of linked list
    while (curr != null) {
        curr = curr.next;
        k++;
    }

    // If n is greater than length
    if (n > k) {
        return head;
    }

    // If head node needs to be deleted
    if (k - n == 0) {
        head = head.next;

        return head;
    }

    // Reach node just before target node
    curr = head;

    for (int i = 1; i < k - n; i++) {
        curr = curr.next;
    }

    // Delete target node
    curr.next = curr.next.next;

    return head;
}

// Function to print linked list
static void printList(Node head)
{
    Node curr = head;

    while (curr != null) {
        System.out.print(curr.data + " ");

        curr = curr.next;
    }
}

// Driver Code
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);

    // Function call
    head = deleteNthNodeFromEnd(head, 2);

    // Print updated linked list
    printList(head);
}

}

Python

Node class

class Node:

def __init__(self, x):
    self.data = x
    self.next = None

Function to delete Nth node from end

def deleteNthNodeFromEnd(head, n):

if head is None:
    return head

k = 0

curr = head

# Find length of linked list
while curr is not None:
    curr = curr.next
    k += 1

# If n is greater than length
if n > k:
    return head

# If head node needs to be deleted
if k - n == 0:
    head = head.next
    return head

# Reach node just before target node
curr = head

for i in range(1, k - n):
    curr = curr.next

# Delete target node
curr.next = curr.next.next

return head

Function to print linked list

def printList(head):

curr = head

while curr is not None:
    print(curr.data, end=" ")

    curr = curr.next

Driver Code

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)

# Function call
head = deleteNthNodeFromEnd(head, 2)

# Print updated linked list
printList(head)

C#

using System;

// Node class class Node { public int data; public Node next;

public Node(int x)
{
    this.data = x;
    this.next = null;
}

}

class GFG { // Function to delete Nth node from end static Node deleteNthNodeFromEnd(Node head, int n) { if (head == null) { return head; }

    int k = 0;

    Node curr = head;

    // Find length of linked list
    while (curr != null) {
        curr = curr.next;
        k++;
    }

    // If n is greater than length
    if (n > k) {
        return head;
    }

    // If head node needs to be deleted
    if (k - n == 0) {
        head = head.next;

        return head;
    }

    // Reach node just before target node
    curr = head;

    for (int i = 1; i < k - n; i++) {
        curr = curr.next;
    }

    // Delete target node
    curr.next = curr.next.next;

    return head;
}

// Function to print linked list
static void printList(Node head)
{
    Node curr = head;

    while (curr != null) {
        Console.Write(curr.data + " ");

        curr = curr.next;
    }
}

// Driver Code
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);

    // Function call
    head = deleteNthNodeFromEnd(head, 2);

    // Print updated linked list
    printList(head);
}

}

JavaScript

// Node class class Node { constructor(x) { this.data = x; this.next = null; } }

// Function to delete Nth node from end function deleteNthNodeFromEnd(head, n) { if (head === null) { return head; }

let k = 0;

let curr = head;

// Find length of linked list
while (curr !== null) {
    curr = curr.next;

    k++;
}

// If n is greater than length
if (n > k) {
    return head;
}

// If head node needs to be deleted
if (k - n === 0) {
    head = head.next;

    return head;
}

// Reach node just before target node
curr = head;

for (let i = 1; i < k - n; i++) {
    curr = curr.next;
}

// Delete target node
curr.next = curr.next.next;

return head;

}

// Function to print linked list function printList(head) { let curr = head;

while (curr !== null) {
    process.stdout.write(curr.data + " ");

    curr = curr.next;
}

}

// Driver Code 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);

// Function call head = deleteNthNodeFromEnd(head, 2);

// Print updated linked list printList(head);

`

[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.

#include using namespace std;

class Node { public: int data; Node *next; Node(int x) { this->data = x; this->next = nullptr; } };

Node *deleteNthNodeFromEnd(Node *head, int n) { Node *dummy = new Node(0); dummy->next = head;

Node *fast = dummy;
Node *slow = dummy;

for (int i = 0; i <= n; i++)
{
    if (fast == nullptr)
        return head;
    fast = fast->next;
}

while (fast != nullptr)
{
    fast = fast->next;
    slow = slow->next;
}

Node *nodeDeleted = slow->next;
if (nodeDeleted != nullptr)
    slow->next = nodeDeleted->next;

delete nodeDeleted;

Node *newHead = dummy->next;
delete dummy;

return newHead;

}

void printList(Node *node) { Node *curr = node; while (curr != nullptr) { cout << curr->data << " "; curr = curr->next; } }

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, 2);

printList(head);
return 0;

}

Java

import java.util.*;

// Node class class Node { int data; Node next;

Node(int x)
{
    this.data = x;
    this.next = null;
}

}

public class GFG {

// Function to delete Nth node from end
static Node deleteNthNodeFromEnd(Node head, int n)
{
    // Create dummy node
    Node dummy = new Node(0);

    dummy.next = head;

    // Initialize slow and fast pointers
    Node slow = dummy;
    Node fast = dummy;

    // Move fast pointer n+1 steps ahead
    for (int i = 0; i <= n; i++) {
        // If n is greater than length
        if (fast == null) {
            return head;
        }

        fast = fast.next;
    }

    // Move both pointers together
    while (fast != null) {
        fast = fast.next;
        slow = slow.next;
    }

    // Node to be deleted
    Node nodeDeleted = slow.next;

    // Delete target node
    if (nodeDeleted != null) {
        slow.next = nodeDeleted.next;
    }

    // Return updated head
    return dummy.next;
}

// Function to print linked list
static void printList(Node head)
{
    Node curr = head;

    while (curr != null) {
        System.out.print(curr.data + " ");

        curr = curr.next;
    }
}

// Driver Code
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);

    // Function call
    head = deleteNthNodeFromEnd(head, 2);

    // Print updated linked list
    printList(head);
}

}

Python

Node class

class Node:

def __init__(self, x):
    self.data = x
    self.next = None

Function to delete Nth node from end

def deleteNthNodeFromEnd(head, n):

# Create dummy node
dummy = Node(0)

dummy.next = head

# Initialize slow and fast pointers
slow = dummy
fast = dummy

# Move fast pointer n+1 steps ahead
for i in range(n + 1):

    # If n is greater than length
    if fast is None:
        return head

    fast = fast.next

# Move both pointers together
while fast is not None:
    fast = fast.next
    slow = slow.next

# Node to be deleted
nodeDeleted = slow.next

# Delete target node
if nodeDeleted is not None:
    slow.next = nodeDeleted.next

# Return updated head
return dummy.next

Function to print linked list

def printList(head):

curr = head

while curr is not None:
    print(curr.data, end=" ")

    curr = curr.next

Driver Code

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)

# Function call
head = deleteNthNodeFromEnd(head, 2)

# Print updated linked list
printList(head)

C#

using System;

// Node class class Node { public int data; public Node next;

public Node(int x)
{
    this.data = x;
    this.next = null;
}

}

class GFG{

// Function to delete Nth node from end
static Node deleteNthNodeFromEnd(Node head, int n)
{
    // Create dummy node
    Node dummy = new Node(0);

    dummy.next = head;

    // Initialize slow and fast pointers
    Node slow = dummy;
    Node fast = dummy;

    // Move fast pointer n+1 steps ahead
    for (int i = 0; i <= n; i++) {
        // If n is greater than length
        if (fast == null) {
            return head;
        }

        fast = fast.next;
    }

    // Move both pointers together
    while (fast != null) {
        fast = fast.next;
        slow = slow.next;
    }

    // Node to be deleted
    Node nodeDeleted = slow.next;

    // Delete target node
    if (nodeDeleted != null) {
        slow.next = nodeDeleted.next;
    }

    // Return updated head
    return dummy.next;
}

// Function to print linked list
static void printList(Node head)
{
    Node curr = head;

    while (curr != null) {
        Console.Write(curr.data + " ");

        curr = curr.next;
    }
}

// Driver Code
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);

    // Function call
    head = deleteNthNodeFromEnd(head, 2);

    // Print updated linked list
    printList(head);
}

}

JavaScript

// Node class class Node { constructor(x) { this.data = x; this.next = null; } }

// Function to delete Nth node from end function deleteNthNodeFromEnd(head, n) { // Create dummy node let dummy = new Node(0);

dummy.next = head;

// Initialize slow and fast pointers
let slow = dummy;
let fast = dummy;

// Move fast pointer n+1 steps ahead
for (let i = 0; i <= n; i++) {
    // If n is greater than length
    if (fast === null) {
        return head;
    }

    fast = fast.next;
}

// Move both pointers together
while (fast !== null) {
    fast = fast.next;
    slow = slow.next;
}

// Node to be deleted
let nodeDeleted = slow.next;

// Delete target node
if (nodeDeleted !== null) {
    slow.next = nodeDeleted.next;
}

// Return updated head
return dummy.next;

}

// Function to print linked list function printList(head) { let curr = head;

while (curr !== null) {
    process.stdout.write(curr.data + " ");

    curr = curr.next;
}

}

// Driver Code 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);

// Function call head = deleteNthNodeFromEnd(head, 2);

// Print updated linked list printList(head);

`