Open In App

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

Similar Reads