Bird
Raised Fist0
DSA Javaprogramming~20 mins

Delete a Node from Circular Linked List in DSA Java - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Circular Linked List Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output after deleting a node with value 3
What is the printed state of the circular linked list after deleting the node with value 3?
DSA Java
class Node {
    int data;
    Node next;
    Node(int d) { data = d; next = null; }
}

class CircularLinkedList {
    Node head;

    void append(int data) {
        Node newNode = new Node(data);
        if (head == null) {
            head = newNode;
            newNode.next = head;
            return;
        }
        Node temp = head;
        while (temp.next != head) {
            temp = temp.next;
        }
        temp.next = newNode;
        newNode.next = head;
    }

    void deleteNode(int key) {
        if (head == null) return;

        Node curr = head, prev = null;

        // If head is to be deleted
        if (head.data == key) {
            if (head.next == head) {
                head = null;
                return;
            }
            Node last = head;
            while (last.next != head) {
                last = last.next;
            }
            head = head.next;
            last.next = head;
            return;
        }

        prev = head;
        curr = head.next;
        while (curr != head && curr.data != key) {
            prev = curr;
            curr = curr.next;
        }

        if (curr.data == key) {
            prev.next = curr.next;
        }
    }

    void printList() {
        if (head == null) {
            System.out.println("List is empty");
            return;
        }
        Node temp = head;
        do {
            System.out.print(temp.data + " -> ");
            temp = temp.next;
        } while (temp != head);
        System.out.println("null");
    }

    public static void main(String[] args) {
        CircularLinkedList cll = new CircularLinkedList();
        cll.append(1);
        cll.append(2);
        cll.append(3);
        cll.append(4);
        cll.deleteNode(3);
        cll.printList();
    }
}
A1 -> 4 -> null
B1 -> 2 -> 3 -> 4 -> null
C2 -> 4 -> 1 -> null
D1 -> 2 -> 4 -> null
Attempts:
2 left
💡 Hint
Remember that deleting a node removes it from the circular chain, and the list remains circular.
❓ Predict Output
intermediate
2:00remaining
Output after deleting the head node
What is the printed state of the circular linked list after deleting the head node (value 1)?
DSA Java
class Node {
    int data;
    Node next;
    Node(int d) { data = d; next = null; }
}

class CircularLinkedList {
    Node head;

    void append(int data) {
        Node newNode = new Node(data);
        if (head == null) {
            head = newNode;
            newNode.next = head;
            return;
        }
        Node temp = head;
        while (temp.next != head) {
            temp = temp.next;
        }
        temp.next = newNode;
        newNode.next = head;
    }

    void deleteNode(int key) {
        if (head == null) return;

        Node curr = head, prev = null;

        if (head.data == key) {
            if (head.next == head) {
                head = null;
                return;
            }
            Node last = head;
            while (last.next != head) {
                last = last.next;
            }
            head = head.next;
            last.next = head;
            return;
        }

        prev = head;
        curr = head.next;
        while (curr != head && curr.data != key) {
            prev = curr;
            curr = curr.next;
        }

        if (curr.data == key) {
            prev.next = curr.next;
        }
    }

    void printList() {
        if (head == null) {
            System.out.println("List is empty");
            return;
        }
        Node temp = head;
        do {
            System.out.print(temp.data + " -> ");
            temp = temp.next;
        } while (temp != head);
        System.out.println("null");
    }

    public static void main(String[] args) {
        CircularLinkedList cll = new CircularLinkedList();
        cll.append(1);
        cll.append(2);
        cll.append(3);
        cll.append(4);
        cll.deleteNode(1);
        cll.printList();
    }
}
A2 -> 3 -> 4 -> null
B1 -> 2 -> 3 -> 4 -> null
C3 -> 4 -> 2 -> null
DList is empty
Attempts:
2 left
💡 Hint
Deleting the head node means the head pointer moves to the next node.
🔧 Debug
advanced
2:00remaining
Identify the error in this deleteNode method
What error will occur when running this deleteNode method on a circular linked list?
DSA Java
void deleteNode(int key) {
    if (head == null) return;

    Node curr = head, prev = null;

    if (head.data == key) {
        if (head.next == head) {
            head = null;
            return;
        }
        Node last = head;
        while (last.next != null) {  // <-- Problem here
            last = last.next;
        }
        head = head.next;
        last.next = head;
        return;
    }

    prev = head;
    curr = head.next;
    while (curr != head && curr.data != key) {
        prev = curr;
        curr = curr.next;
    }

    if (curr.data == key) {
        prev.next = curr.next;
    }
}
AInfinite loop because last.next never becomes null in circular list
BNullPointerException when head is null
CNo error, code runs correctly
DArrayIndexOutOfBoundsException due to wrong indexing
Attempts:
2 left
💡 Hint
Think about the condition in the while loop for finding the last node in a circular linked list.
❓ Predict Output
advanced
2:00remaining
Output after deleting the only node in the list
What is the output after deleting the only node (value 5) in the circular linked list?
DSA Java
class Node {
    int data;
    Node next;
    Node(int d) { data = d; next = null; }
}

class CircularLinkedList {
    Node head;

    void append(int data) {
        Node newNode = new Node(data);
        if (head == null) {
            head = newNode;
            newNode.next = head;
            return;
        }
        Node temp = head;
        while (temp.next != head) {
            temp = temp.next;
        }
        temp.next = newNode;
        newNode.next = head;
    }

    void deleteNode(int key) {
        if (head == null) return;

        if (head.data == key) {
            if (head.next == head) {
                head = null;
                return;
            }
            Node last = head;
            while (last.next != head) {
                last = last.next;
            }
            head = head.next;
            last.next = head;
            return;
        }

        Node prev = head;
        Node curr = head.next;
        while (curr != head && curr.data != key) {
            prev = curr;
            curr = curr.next;
        }

        if (curr.data == key) {
            prev.next = curr.next;
        }
    }

    void printList() {
        if (head == null) {
            System.out.println("List is empty");
            return;
        }
        Node temp = head;
        do {
            System.out.print(temp.data + " -> ");
            temp = temp.next;
        } while (temp != head);
        System.out.println("null");
    }

    public static void main(String[] args) {
        CircularLinkedList cll = new CircularLinkedList();
        cll.append(5);
        cll.deleteNode(5);
        cll.printList();
    }
}
Anull
B5 -> null
CList is empty
D5 -> 5 -> null
Attempts:
2 left
💡 Hint
Deleting the only node should empty the list.
🧠 Conceptual
expert
2:00remaining
Why is it important to update the last node's next pointer when deleting the head?
In a circular linked list, when deleting the head node, why must we update the last node's next pointer to the new head?
ATo avoid memory leaks by freeing the last node
BTo maintain the circular link so the list remains connected
CTo reset the head pointer to null automatically
DTo sort the list after deletion
Attempts:
2 left
💡 Hint
Think about what keeps the circular linked list circular.

Practice

(1/5)
1. What happens when you delete the only node in a circular linked list?
easy
A. The list becomes empty (head points to null)
B. The list still contains the deleted node
C. The list forms a linear linked list
D. The list throws an error automatically

Solution

  1. Step 1: Understand single node deletion

    When the only node is deleted, no nodes remain in the list.
  2. Step 2: Resulting list state

    The head pointer is set to null, indicating an empty list.
  3. Final Answer:

    The list becomes empty (head points to null) -> Option A
  4. Quick Check:

    Deleting last node empties list [OK]
Hint: Deleting last node empties list, head = null [OK]
Common Mistakes:
  • Assuming list still has nodes after deletion
  • Not setting head to null
  • Confusing circular with linear list after deletion
2. Which of the following is the correct way to update the head when deleting the head node in a circular linked list?
easy
A. head = tail; tail.next = null;
B. head = null; tail = null;
C. head = head.prev; tail.next = head;
D. head = head.next; tail.next = head;

Solution

  1. Step 1: Update head pointer

    When deleting the head node, move head to next node.
  2. Step 2: Maintain circular link

    Update tail's next pointer to new head to keep circular structure.
  3. Final Answer:

    head = head.next; tail.next = head; -> Option D
  4. Quick Check:

    Head moves forward, tail links to new head [OK]
Hint: After deleting head, point tail.next to new head [OK]
Common Mistakes:
  • Setting tail.next to null breaks circular link
  • Setting head to null incorrectly
  • Using head.prev which doesn't exist in singly circular list
3. What will be the output after deleting node with value 3 from this circular linked list: 1 -> 2 -> 3 -> 4 -> (back to 1)?
Node head = new Node(1);
Node n2 = new Node(2);
Node n3 = new Node(3);
Node n4 = new Node(4);
head.next = n2; n2.next = n3; n3.next = n4; n4.next = head;
// Delete node with value 3
// After deletion, print list starting from head
medium
A. 1 -> 2 -> 4 -> (back to 1)
B. 1 -> 2 -> 3 -> 4 -> (back to 1)
C. 1 -> 4 -> (back to 1)
D. 2 -> 4 -> (back to 2)

Solution

  1. Step 1: Locate node with value 3

    Traverse list: 1 -> 2 -> 3 found.
  2. Step 2: Remove node 3 by linking 2 to 4

    Set n2.next = n3.next (which is n4), skipping node 3.
  3. Final Answer:

    1 -> 2 -> 4 -> (back to 1) -> Option A
  4. Quick Check:

    Node 3 removed, list links 2 to 4 [OK]
Hint: Skip node by linking previous to next [OK]
Common Mistakes:
  • Not updating previous node's next pointer
  • Breaking circular link by missing tail update
  • Deleting wrong node or skipping nodes
4. Identify the error in this code snippet that deletes a node with value 'key' from a circular linked list:
Node current = head;
while(current.next != head && current.next.data != key) {
    current = current.next;
}
if(current.next.data == key) {
    current.next = current.next.next;
}
medium
A. Does not handle deletion when list has only one node
B. Infinite loop if key not found
C. Does not update tail pointer when deleting last node
D. Deletes head node incorrectly

Solution

  1. Step 1: Check tail update on deletion

    If deleted node is tail, tail pointer must update to current node.
  2. Step 2: Code misses tail update

    Code only changes current.next, tail remains pointing to deleted node.
  3. Final Answer:

    Does not update tail pointer when deleting last node -> Option C
  4. Quick Check:

    Tail pointer must update if last node deleted [OK]
Hint: Update tail if deleting last node [OK]
Common Mistakes:
  • Ignoring tail pointer update
  • Assuming tail auto-updates
  • Not checking if deleted node is tail
5. Given a circular linked list with nodes 10 -> 20 -> 30 -> 40 -> (back to 10), how would you delete the node with value 10 (head node) and maintain the circular structure? Choose the correct sequence of operations.
hard
A. Set head = null; tail = null;
B. Set head = head.next; tail.next = head; delete old head;
C. Set tail.next = head.next; head = head.next;
D. Set tail = head; head = head.next; tail.next = null;

Solution

  1. Step 1: Move head to next node

    Set head = head.next to remove old head from start.
  2. Step 2: Update tail's next pointer

    Set tail.next = head to maintain circular link.
  3. Step 3: Delete old head node properly

    Remove old head node from memory if needed.
  4. Final Answer:

    Set head = head.next; tail.next = head; delete old head; -> Option B
  5. Quick Check:

    Head moves forward, tail links to new head [OK]
Hint: Update head and tail.next to new head when deleting head [OK]
Common Mistakes:
  • Breaking circular link by setting tail.next to null
  • Not moving head pointer
  • Deleting head without updating tail.next
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard