In this post, We will learn How to remove the last node from a Singly Linked List in Java?
Program Logic to Remove/Delete Last node from Singly Linked List:

If we want to delete the last node of a linked list then find the second last node and make the next pointer of that node null.
Node<T> lastNode = head;
Node<T> previousToLastNode = null;
while(lastNode.next != null) {
previousToLastNode = lastNode;
lastNode = lastNode.next;
}
previousToLastNode.next = null;
Complete Source Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
package com.kkjavatutorials.util; import java.util.NoSuchElementException; /** * How to remove last node from a Singly Linked List in Java ? * @author KK JavaTutorials */ public class LinkedList<T> { private Node<T> head; //This internal class represents Node in Linked List private static class Node<T>{ private T data; private Node<T> next; public Node(T data) { super(); this.data = data; this.next = null; } } /** * This method insert a Node in Linked List * @param data has to insert in the List */ public void insert(T data) { Node<T> newNode = new Node<T>(data); if(head == null) { head = newNode; }else { Node<T> currentNode = head; while (currentNode.next != null) { currentNode = currentNode.next; } currentNode.next = newNode; } } /** * Removes and returns the last element from this list. * @return the last element from this list * @throws NoSuchElementException if this list is empty */ public T removeLast() { if(head == null) { throw new NoSuchElementException("List is Empty!!"); } Node<T> lastNode = head; Node<T> previousToLastNode = null; while(lastNode.next != null) { previousToLastNode = lastNode; lastNode = lastNode.next; } previousToLastNode.next = null; return lastNode.data; } /** * Method which traverse Linked List and display all data. */ public void displayLinkedList() { Node<T> currentNode = head; while(currentNode!= null) { System.out.print(currentNode.data+" "); currentNode = currentNode.next; } } public static void main(String[] args) { LinkedList<Integer> linkedList = new LinkedList<>(); linkedList.insert(10); linkedList.insert(20); linkedList.insert(30); linkedList.insert(40); linkedList.insert(50); linkedList.insert(60); System.out.println("Original LinkedList:"); linkedList.displayLinkedList(); System.out.println(); Integer removeLast = linkedList.removeLast(); System.out.println("After removing last Element:"+removeLast); linkedList.displayLinkedList(); } } |
Output Of Above Program :
Original LinkedList:
10 20 30 40 50 60
After removing last Element:60
10 20 30 40 50
You May Also Like:
How to remove the first node from a Singly Linked List in Java ?
How to find Nth node from the end of a Singly Linked List in Java?
How to insert a node in Linked List at a given position in Java ?
That’s all about the How to remove the last node from a Singly Linked List in Java?
If you have any feedback or suggestion please feel free to drop in below comment box.