Delete /remove all nodes of a single linked list in java (example/non-recursive)

  • Given a single linked list, we will delete all nodes of single linked list
    • i.e. We want to delete the single linked list.
  • We have shown the single linked list in Fig 1.
    • We will traverse the single linked list.
    • We will delete the nodes of single linked list during traversal.
    • At end of iteration the whole linked list will be deleted.
Fig 1: Delete single linked list
  • We will simulate the deletion of head node from linked list
    • Similar procedure can be followed for rest of the nodes of linked list.
  • We have shown the deletion of node in Fig 2.
    • One thing, we need to take care that before we delete any node, we have to ensure that we store its next reference (next node in linked list) , otherwise we will lose the single linked list.

Algorithm – delete/remove all nodes of a single linked list in java

  • Create the variable Node backup = null
    • backup variable will be used to store the reference of next node of linked list
  • Start the traversing the single linked list using head reference
  • Store the reference of next node i.e. Node 5 [Step 1 shown in Fig 2]
    • backup = head.next
    • backup will start pointing to Node 5
  • Set the head reference to null
    • head = null (GC will take care of deleting it)
  • Restore the head to next node
    • head = backup
    • So that we can delete rest of nodes in linked list
  • Keep on iterating through the single linked list
  • Once we finished the iteration, Complete linked list will be deleted.
delete remove node single linked list
Fig 2: Delete head node of single linked list

Time complexity of algorithm is O(n).

Program – Delete/remove all nodes of single linked list in java.

1.) DeleteLinkedList Class:

  • delete method method delete all nodes in a single linked list (one by one)
  • insert method is used to insert elements or node to a single linked list
    • insert function is used to create linked list
  • print method is used to print the single linked list
package org.learn.Question;

import org.learn.List.Node;

public class DeleteLinkedList {

 public static Node delete(Node head) {
  Node backup = null;
  while (head != null) {
   backup = head.next;
   head = null;
   head = backup;
  }
  return head;
 }

 public static void insert(Node head, int data) {
  while (head.next != null)
   head = head.next;
  head.next = new Node(data);
 }

 public static void print(Node head) {
  while (head != null) {
   System.out.printf("%d ", head.data);
   head = head.next;
  }
  System.out.println("");
 }
}

2.) App Class:

  • We are creating the single linked list using DeleteLinkedList.insert method.
  • We are printing the linked list using DeleteLinkedList.print method.
  • We are calling DeleteLinkedList.delete method, to delete all nodes in a single linked list
package org.learn.Client;

import org.learn.List.Node;
import org.learn.Question.DeleteLinkedList;

public class App {
 public static void main(String[] args) {
  int[] data = { 9, 5, 2, 8, 10, 9, 1 };
  Node head = new Node(data[0]);
  for (int count = 1; count < data.length; count++)
   DeleteLinkedList.insert(head, data[count]);

  System.out.printf("Linked list is : ");
  DeleteLinkedList.print(head);

  head = DeleteLinkedList.delete(head);
  if (head == null) {
   System.out.println("Deleted all nodes in linked list");
  } else {
   System.out.println("Could not able to delete all nodes from linked list");
  }
 }
}

3.) Node Class:

  • Node class representing the node(s) of single linked list
package org.learn.List;

public class Node {
 public int data;
 public Node next;

 public Node(int num) {
  this.data = num;
  this.next = null;
 }
}

Output – delete/remove all nodes of  a single linked list in java

Linked list is : 10 20 10 20 50 20 50
Deleted all nodes in linked list

Download code – delete/ remove all nodes of single linked list in java

 

 

Scroll to Top