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();
}
}