00:00

Deletion in Singly Linked List

Last updated:

Deleting from a singly linked list means unlinking a node: the node before it is made to point past it, and the removed node, no longer referenced, is reclaimed by Java's garbage collector. The only hard part is reaching the node before the one you want to remove, because a singly linked list cannot step backwards.

This tutorial covers deletion from three places:

  1. The beginning of the list (the head).

  2. A given position in the middle of the list.

  3. The end of the list (the tail).

Create a node class

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

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

1. Delete a node from the beginning of the linked list

Removing the first node only requires moving head to the second node, so it takes O(1) time. If the list is empty there is nothing to delete.

DeleteNodeAtBeginningInSLL.java
// Java program to delete a node from the beginning of the linked list
public class DeleteNodeAtBeginningInSLL {
  // Method to delete the head node
  static Node deleteHead(Node head) {
    // Nothing to delete in an empty list
    if (head == null) {
      System.out.println("List is empty");
      return null;
    }

    // Move head to the second node; the old first node is no longer
    // referenced, so the garbage collector will reclaim it
    head = head.next;

    // Return the new head
    return head;
  }

  // Method to print the linked list
  public static void printList(Node head) {
    while (head != null) {
      System.out.print(head.data + " -> ");
      head = head.next;
    }
    System.out.println("null");
  }

  public static void main(String[] args) {
    // Create nodes
    Node head = new Node(10);
    head.next = new Node(20);
    head.next.next = new Node(30);
    head.next.next.next = new Node(40);

    System.out.println("Original Linked List data: ");
    printList(head);

    // Deleting the head node
    head = deleteHead(head);
    System.out.println("After deleting head, Linked List data: ");
    printList(head);
  }
}
Output
Original Linked List data:
10 -> 20 -> 30 -> 40 -> null
After deleting head, Linked List data:
20 -> 30 -> 40 -> null

2. Delete a node from the middle of the linked list

To delete the node at position p (counting from 1), walk two pointers forward: temp to the node being deleted and prev to the node just before it. Then set prev.next = temp.next. Position 1 is the head and is handled separately. Reaching the position takes O(n) time in the worst case.

DeleteNodeAtMiddleInSLL.java
// Java program to delete a node from the linked list at the given position
public class DeleteNodeAtMiddleInSLL {

  // Method to delete a node at a given position
  public static Node deleteNode(Node head, int position) {
    // Nothing to delete in an empty list
    if (head == null) {
      System.out.println("List is empty");
      return head;
    }

    // Positions start at 1
    if (position < 1) {
      System.out.println("Invalid position.");
      return head;
    }

    Node temp = head;
    Node prev = null;

    // Case 1: Head is to be deleted
    if (position == 1) {
      head = temp.next;
      return head;
    }

    // Case 2: Node to be deleted is in middle
    // Traverse till given position
    for (int i = 1; temp != null && i < position; i++) {
      prev = temp;
      temp = temp.next;
    }

    // If given position is found, delete node
    if (temp != null) {
      prev.next = temp.next;
    } else {
      System.out.println("Position is out of bounds.");
    }

    return head;
  }

  // Method to print a list
  public static void printList(Node head) {
    while (head != null) {
      System.out.print(head.data + " -> ");
      head = head.next;
    }
    System.out.println("null");
  }

  public static void main(String[] args) {
    // Create nodes
    Node head = new Node(10);
    head.next = new Node(20);
    head.next.next = new Node(30);
    head.next.next.next = new Node(40);
    head.next.next.next.next = new Node(50);

    System.out.println("Original Linked List data:");
    printList(head);

    // Deleting node at position 3
    head = deleteNode(head, 3);

    // Print list after deletion
    System.out.print("After deletion, Linked List data:\n");
    printList(head);
  }
}
Output
Original Linked List data:
10 -> 20 -> 30 -> 40 -> 50 -> null
After deletion, Linked List data:
10 -> 20 -> 40 -> 50 -> null

3. Delete a node from the end of the linked list

To remove the last node, find the second-last node (the one whose next.next is null) and set its next to null. A list with a single node becomes empty. Because the walk covers the whole list, this is O(n).

DeleteNodeAtEndInSLL.java
// Java program to delete the end or last node of linked list
public class DeleteNodeAtEndInSLL {
  // Method to remove the last node from the linked list
  static Node removeLastNode(Node head) {

    // Nothing to delete in an empty list
    if (head == null) {
      System.out.println("List is empty");
      return null;
    }

    // If the list has only one node, delete it and return null
    if (head.next == null) {
      return null;
    }

    // Find the second last node
    Node secondLast = head;
    while (secondLast.next.next != null) {
      secondLast = secondLast.next;
    }

    // Delete the last node
    secondLast.next = null;

    return head;
  }

  // Method to print the list
  public static void printList(Node head) {
    while (head != null) {
      System.out.print(head.data + " -> ");
      head = head.next;
    }
    System.out.println("null");
  }

  public static void main(String[] args) {
    // Create nodes
    Node head = new Node(10);
    head.next = new Node(20);
    head.next.next = new Node(30);
    head.next.next.next = new Node(40);

    System.out.println("Original Linked List data: ");
    printList(head);

    // Removing the last node from the list
    head = removeLastNode(head);

    System.out.println("After removing last node, Linked List data: ");
    printList(head);
  }
}
Output
Original Linked List data:
10 -> 20 -> 30 -> 40 -> null
After removing last node, Linked List data:
10 -> 20 -> 30 -> null