Deletion from end in Singly Linked List in Java

Java Program for Deletion from end in Singly Linked List

Here, you will learn how Deletion from End in Singly Linked List in Java works through simple explanations, proper algorithms, and complete Java code examples.

This problem is frequently asked in coding interviews because it tests pointer logic and edge case handling.

Deletion from end in Singly Linked List in Java

In a singly linked list, deleting from the end means removing the last node and making the second last node the new tail.

Example:

10 → 20 → 30 → 40

After deleting the last node (40):

10 → 20 → 30

Understanding Deletion from End in Singly Linked List in Java helps you learn:

  • How to traverse nodes
  • How to detect the last and second-last nodes
  • Edge case handling like deleting from an empty list or single-node list
  • Pointer manipulation

This is also a base operation for more advanced tasks like deleting nth node from end, reversing, and circular list handling.

Deletion-at-the-end-of-the-Singly-Linked-List-in-Java

Approaches to Deletion from End in Singly Linked List in Java

We will cover 2 methods:

Each approach includes:

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

Methods for Deletion from End in Singly Linked List in Java

Method 1: Iterative Traversal to Find the Second Last Node

Algorithm:

  1. If head is null, return null (empty list).
  2. If head.next is null, return null (single node list).
  3. Initialize temp = head.
  4. Traverse until temp.next.next == null (stop at second-last node).
  5. Set temp.next = null.
  6. Return updated head.

Java Code:

Run
class DeleteEnd {

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

    public static Node deleteFromEnd(Node head) {
        if (head == null) return null;

        if (head.next == null) {
            return null;
        }

        Node temp = head;
        while (temp.next.next != null) {
            temp = temp.next;
        }

        temp.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(5);
        head.next = new Node(15);
        head.next.next = new Node(25);

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

        head = deleteFromEnd(head);

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

Input:

5 15 25

Output:

5 15

Methods for Deletion from End in Singly Linked List in Java

Method 2: Two Traversal Approach (Find Length First)

Algorithm:

  1. If head is null, return null.
  2. Traverse once to calculate list length len.
  3. If length is 1, return null.
  4. Traverse again to (len – 1)th node (second-last).
  5. Set its next to null.
  6. Return head.

Java Code:

Run
class DeleteEndLengthMethod {

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

    public static Node deleteLast(Node head) {
        if (head == null) return null;

        int len = 0;
        Node temp = head;
        while (temp != null) {
            len++;
            temp = temp.next;
        }

        if (len == 1) return null;

        temp = head;
        for (int i = 1; i < len - 1; i++) {
            temp = temp.next;
        }

        temp.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);

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

        head = deleteLast(head);

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

Input:

10 20 30

Output:

10 20

Conclusion....

Deletion from the end of a singly linked list is a fundamental operation that demonstrates how pointer based structures work without random access because nodes must be accessed sequentially, identifying the second last node is essential before deletion.

MethodTime ComplexitySpace Complexity
Iterative (Second Last Node)O(n)O(1)
Two Traversal Length MethodO(n)O(1)

This highlights one of the key characteristics of linked lists:

  • Efficient insertions and deletions only when node references are known.
  • Both methods discussed perform the same task with linear time complexity and constant space.
  • The approach of deleting from the end forms the basis for more advanced operations such as deleting the nth node from the end, reversing a list, and implementing queue like structures.

Understanding how Deletion from End in Singly Linked List in Java works builds a stronger foundation for mastering linked list manipulation and algorithmic reasoning.

FAQ's related to Deletion from End in Singly Linked List in Java

Answer:

It means removing the last node and updating the second last node to become the new tail.

Answer:

You traverse the list until you reach the second last node and set its next pointer to null.

Answer:

Yes, it is linear time O(n), which is required because traversal is necessary.

Answer:

No, only pointer updates are needed, so space complexity is O(1).

Answer:

Because a singly linked list does not have backward links, so traversal is required to reach the last 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