Java Program for deletion from the end of a Singly Linked List
Compilation of Data Elements of similar types with the address to the next element is termed as a linked list. To perform a deletion in a linked list at the end we need to change the address in the pointer location of the second last node to NULL. Deletion in a linked list is nothing but redirection of pointers from one address reference to other.
Implementation
First We’ve to check that the list is not empty, if yes then exit.
Now the pointer from the second last node will be redirected towards the Null pointer.
If there is only one node in the linked list then the head will point to the null.
import java.lang.*;
class LinkedList {
Node head;
// Node Class
class Node{
int data;
Node next;
Node(int x) // parameterized constructor
{
data = x;
next = null;
}
}
public void deleteLast()
{
if (head == null){
System.out.println("List is empty, not possible to delete");
return;
}
// if LL has only one node
if(head.next == null)
{
System.out.println("Deleted: " + head.data);
head = head.next;
}
Node previous = null;
Node temp = head;
// else traverse to the last node
while (temp.next != null)
{
// store previous link node as we need to change its next val
previous = temp;
temp = temp.next;
}
// Curr assign 2nd last node's next to Null
System.out.println("Deleted: " + temp.data);
previous.next = null;
// 2nd last now becomes the last node
}
public Node insert(int data)
{
// Creating newNode memory & assigning data value
Node newNode = new Node(data);
// assigning this newNode's next as current head node
newNode.next = head;
// re-assigning head to this newNode
head = newNode;
return head;
}
public void display()
{
Node node = head;
//as linked list will end when Node reaches Null
while(node!=null)
{
System.out.print(node.data + " ");
node = node.next;
}
System.out.println("\n");
}
}
public class Main
{
public static void main(String args[])
{
LinkedList ll = new LinkedList();
ll.insert(12);
ll.insert(11);
ll.insert(10);
ll.insert(9);
ll.insert(8);
ll.insert(7);
ll.display();
ll.deleteLast();
ll.deleteLast();
ll.deleteLast();
ll.display();
}
}
import java.lang.*;
// Node Class
class Node {
int data;
Node next;
Node(int x) // parameterized constructor
{
data = x;
next = null;
}
}
class Main
{
static Node deleteLast(Node head) {
if (head == null){
System.out.println("List is empty, not possible to delete");
return head;
}
// if LL has only one node
if(head.next == null)
{
System.out.println("Deleted: " + head.data);
head = head.next;
return head;
}
Node previous = null;
Node temp = head;
// else traverse to the last node
while (temp.next != null)
{
// store previous link node as we need to change its next val
previous = temp;
temp = temp.next;
}
// Curr assign 2nd last node's next to Null
System.out.println("Deleted: " + temp.data);
previous.next = null;
// 2nd last now becomes the last node
return head;
}
public static Node insert(Node head, int data)
{
// Creating newNode memory & assigning data value
Node newNode = new Node(data);
// assigning this newNode's next as current head node
newNode.next = head;
// re-assigning head to this newNode
head = newNode;
return head;
}
static void display(Node node) {
//as linked list will end when Node is Null
while (node != null) {
System.out.print(node.data + " ");
node = node.next;
}
System.out.println("\n");
}
public static void main(String args[])
{
Node head = null;
head = insert(head, 12);
head = insert(head, 11);
head = insert(head, 10);
head = insert(head, 9);
head = insert(head, 8);
head = insert(head, 7);
display(head);
head = deleteLast(head);
head = deleteLast(head);
head = deleteLast(head);
display(head);
}
}
Login/Signup to comment