Deletion of nth node in Linked List in Java

Java Program for Deletion of nth node in Linked List

In this guide, you will learn how to Delete nth Node in Linked List in Java using simple explanations, clear algorithms, and complete Java code.

Deleting a specific node from a linked list is an essential operation in data structures. This problem is frequently asked in coding interviews because it tests your understanding of pointer manipulation and edge-case handling.

Delete nth node in Linked List in Java

What is deletion of nth node in Linked List in Java?

Given a singly linked list and a position n, your task is to remove the node present at the nth position (1-based index).

Example:

10 → 20 → 30 → 40 → 50

If n = 3, remove the third node (30):

10 → 20 → 40 → 50

Understanding Delete nth Node in Linked List in Java helps you learn:

  • Node traversal logic
  • Boundary condition handling
  • Pointer updates
  • Efficient node deletion

This operation strengthens your base for more complex linked list operations like deleting from end, folding lists, and reversing.

deletion-of-nth-node-in-Singly-Linked-List-in-Java

Method for Deletion of nth Node in Linked List in Java

Deletion of nth Node in Linked List in Java Programming can be performed using 2 methods, mentioned as follows:

  1. Method 1: Iterative Traversal (Simple Approach)

  2. Method 2: Using Two Pointers

Each method includes:

  • Step by step algorithm
  • Java code
  • Example input and output
  • Time and space complexity

Methods for Deletion of nth node in Linked List in Java

Method 1: Simple Iterative Traversal

Algorithm:

  1. If head is null, return null.
  2. If n == 1, update head to head.next and return new head.
  3. Initialize temp = head.
  4. Move temp to the (n – 2)th node using a loop.
  5. Let target = temp.next be the node to delete.
  6. Point temp.next to target.next.
  7. Disconnect target.next.
  8. Return updated head.

Java Code:

Run
class DeleteNthNode {

    static class Node {
        int data;
        Node next;
        Node(int data) { this.data = data; }
    }

    public static Node deleteNth(Node head, int n) {
        if (head == null) return null;

        if (n == 1) {
            Node newHead = head.next;
            head.next = null;
            return newHead;
        }

        Node temp = head;
        for (int i = 1; i < n - 1 && temp != null; i++) {
            temp = temp.next;
        }

        if (temp == null || temp.next == null) return head;

        Node target = temp.next;
        temp.next = target.next;
        target.next = null;

        return head;
    }

    public static void printList(Node head) {
        while (head != null) {
            System.out.print(head.data + " ");
            head = head.next;
        }
        System.out.println();
    }

    public static void main(String[] args) {
        Node head = new Node(10);
        head.next = new Node(20);
        head.next.next = new Node(30);
        head.next.next.next = new Node(40);

        System.out.println("Before deletion:");
        printList(head);

        head = deleteNth(head, 3);

        System.out.println("After deletion:");
        printList(head);
    }
}

Input:

10 20 30 40
n = 3

Output:

10 20 40

Method for Deletion of nth node in Linked List in Java

Method 2: Two Pointer Technique

Algorithm:

  1. Create a dummy node pointing to head (helps handle n = 1 easily).

  2. Initialize two pointers: fast = dummy, slow = dummy.

  3. Move fast ahead by n nodes.

  4. Move both pointers until fast.next becomes null.

  5. Now slow.next is the nth node to delete.

  6. Skip the node: slow.next = slow.next.next.

  7. Return dummy.next (updated head).

Java Code:

Run
class DeleteNthNodeTwoPointer {

    static class Node {
        int data;
        Node next;
        Node(int data) { this.data = data; }
    }

    public static Node deleteNth(Node head, int n) {
        Node dummy = new Node(0);
        dummy.next = head;

        Node fast = dummy, slow = dummy;

        for (int i = 0; i < n && fast != null; i++) {
            fast = fast.next;
        }

        if (fast == null) return head;

        while (fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }

        slow.next = slow.next.next;

        return dummy.next;
    }

    public static void printList(Node head) {
        while (head != null) {
            System.out.print(head.data + " ");
            head = head.next;
        }
        System.out.println();
    }

    public static void main(String[] args) {
        Node head = new Node(5);
        head.next = new Node(15);
        head.next.next = new Node(25);
        head.next.next.next = new Node(35);

        System.out.println("Before deletion:");
        printList(head);

        head = deleteNth(head, 2);

        System.out.println("After deletion:");
        printList(head);
    }
}

Input:

5 15 25 35
n = 2

Output:

5 25 35

Comparison between Methods to Delete nth node in Linked List in Java

MethodTime ComplexitySpace Complexity
Simple Iterative TraversalO(n)O(1)
Two-Pointer TechniqueO(n)O(1)

Deleting the nth node from a linked list is a fundamental operation in data structures, and mastering it helps you build a strong foundation for advanced linked list manipulation.

The operation requires careful pointer adjustments because removing a node affects the links between its neighbors.

  • Both iterative traversal and two-pointer techniques achieve the same goal, but the two pointer method generalizes better for additional variations such as deleting from the end.
  • Since linked lists rely on pointers instead of index based access, understanding deletion logic is essential for efficient list modification.

Overall, knowing how to Delete nth Node in Linked List in Java improves your understanding of memory management, list traversal patterns, and edge-case handling in linear data structures.

FAQ's related to Deletion of nth node in Linked List in Java

Answer:

It means removing the node present at position n and reconnecting the surrounding nodes properly.

Answer:

You traverse the list to the (n-1)th node, then skip the nth node using pointer manipulation.

Answer:

Then the head node is removed and the next node becomes the new head.

Answer:

The operation is ignored because nth node does not exist.

Answer:

It simplifies edge cases like deleting the first node.

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Singly Linked List

  • Introduction to Linked List in Data Structure
    Click Here
  • Linked List in –
    C | C++ | Java
  • Singly Linked List in –
    C | C++ | Java
  • Insertion in singly Linked List –
    C | C++ | Java
  • Insertion at beginning in singly Linked List  –
    C | C++Java
  • Insertion at nth position in singly Linked List  –
    C | C++Java
  • Insertion at end in singly Linked List  –
    C | C++Java
  • Deletion in singly Linked List  –
    C | C++Java
  • Deletion from beginning in singly linked list :
    C | C++ | Java
  • Deletion from nth position in singly linked list :
    C | C++ | Java
  • Deletion from end in singly linked list :
    C | C++ | Java
  • Linked List Insertion and Deletion –
    C | C++Java
  • Reverse a linked list without changing links between nodes (Data reverse only) –
    C | C++Java
  • Reverse a linked list by changing links between nodes –
    C | C++Java
  • Print reverse of a linked list without actually reversing –
    C |C++Java
  • Print reverse of a linked list without actually reversing –
    C |C++Java
  • Insertion in the middle Singly Linked List –
    C | C++Java
  • Insertion in a Sorted Linked List –
    C | C++Java
  • Delete alternate nodes of a Linked List –
    C | C++Java
  • Find middle of the linked list –
    C | C++Java
  • Reverse a linked list in groups of given size –
    C | C++Java
  • Find kth node from end of the linked list –
    C | C++Java
  • Append the last n nodes of a linked list to the beginning of the list –
    C | C++Java
  • Check whether linked list is palindrome or not –
    C | C++Java
  • Fold a Linked List –
    C | C++Java
  • Insert at given Position –
    C | C++Java
  • Deletion at given Position –
    C | C++Java

Singly Linked List

  • Introduction to Linked List in Data Structure
  • Linked List in – C | C++ | Java
  • Singly Linked List in – C | C++ | Java
  • Insertion in singly Linked List – C | C++ | Java
    • Insertion at beginning in singly Linked List  – C | C++Java
    • Insertion at nth position in singly Linked List  – C | C++Java
    • Insertion at end in singly Linked List  – C | C++Java
  • Deletion in singly Linked List  – C | C++Java
    • Deletion from beginning in singly linked list : C | C++ | Java
    • Deletion from nth position in singly linked list : C | C++ | Java
    • Deletion from end in singly linked list : C | C++ | Java
  • Reverse a linked list without changing links between nodes (Data reverse only) – C | C++Java
  • Linked List Insertion and Deletion – C | C++Java
  • Reverse a linked list by changing links between nodes – C | C++Java
  • Linked List insertion in the middle – C | C++Java
  • Print reverse of a linked list without actually reversing – C |C++ | Java
  • Search an element in a linked list – C | C++Java
  • Insertion in a Sorted Linked List – C | C++Java
  • Delete alternate nodes of a Linked List – C | C++Java
  • Find middle of the linked list – C | C++Java
  • Reverse a linked list in groups of given size – C | C++Java
  • Find kth node from end of the linked list – C | C++Java
  • Append the last n nodes of a linked list to the beginning of the list – C | C++Java
  • Check whether linked list is palindrome or not – C | C++Java
  • Fold a Linked List – C | C++Java
  • Insert at a given position – C | C++Java
  • Delete at a given position – C | C++Java