WikiGalaxy

Personalize

Deletion in Singly Linked List: All Cases

Introduction:

Deletion in a singly linked list involves removing a node from the list, and it can be categorized into three main cases: deletion at the beginning, deletion at the end, and deletion at a specific position. Let's explore these with detailed examples.

Example 1: Deletion at the Beginning

Step-by-Step Diagram:

      Step 1: Initial List
      [Head] -> [10 | Next] -> [20 | Next] -> [30 | Null]

      Step 2: Remove Head
      [Head] -> [20 | Next] -> [30 | Null]
    

Code Implementation:


        class Node {
          int data;
          Node next;
          Node(int data) { this.data = data; }
        }

        class LinkedList {
          Node head;

          void deleteAtBeginning() {
            if (head != null) {
              head = head.next;
            }
          }
        }
      

Example 2: Deletion at the End

Step-by-Step Diagram:

      Step 1: Initial List
      [Head] -> [10 | Next] -> [20 | Next] -> [30 | Null]

      Step 2: Remove Last Node
      [Head] -> [10 | Next] -> [20 | Null]
    

Code Implementation:


        class LinkedList {
          Node head;

          void deleteAtEnd() {
            if (head == null) return;
            if (head.next == null) {
              head = null;
              return;
            }
            Node secondLast = head;
            while (secondLast.next.next != null) {
              secondLast = secondLast.next;
            }
            secondLast.next = null;
          }
        }
      

Example 3: Deletion at a Specific Position

Step-by-Step Diagram:

      Step 1: Initial List
      [Head] -> [10 | Next] -> [20 | Next] -> [30 | Null]

      Step 2: Remove Node at Position 2
      [Head] -> [10 | Next] -> [30 | Null]
    

Code Implementation:


        class LinkedList {
          Node head;

          void deleteAtPosition(int position) {
            if (head == null) return;
            if (position == 0) {
              head = head.next;
              return;
            }
            Node current = head;
            for (int i = 0; current != null && i < position - 1; i++) {
              current = current.next;
            }
            if (current == null || current.next == null) return;
            current.next = current.next.next;
          }
        }
      

Example 4: Deletion of a Node by Key

Step-by-Step Diagram:

      Step 1: Initial List
      [Head] -> [10 | Next] -> [20 | Next] -> [30 | Null]

      Step 2: Remove Node with Key 20
      [Head] -> [10 | Next] -> [30 | Null]
    

Code Implementation:


        class LinkedList {
          Node head;

          void deleteByKey(int key) {
            Node temp = head, prev = null;
            if (temp != null && temp.data == key) {
              head = temp.next;
              return;
            }
            while (temp != null && temp.data != key) {
              prev = temp;
              temp = temp.next;
            }
            if (temp == null) return;
            prev.next = temp.next;
          }
        }
      

Example 5: Deletion of the Entire List

Step-by-Step Diagram:

      Step 1: Initial List
      [Head] -> [10 | Next] -> [20 | Next] -> [30 | Null]

      Step 2: Remove All Nodes
      [Head] -> Null
    

Code Implementation:


        class LinkedList {
          Node head;

          void deleteList() {
            head = null;
          }
        }
      
logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025