Bird
Raised Fist0
DSA C#programming~20 mins

Delete from End of Doubly Linked List in DSA C# - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Doubly Linked List Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output after deleting the last node?
Consider a doubly linked list with nodes 10 <-> 20 <-> 30 <-> 40. After deleting the last node, what is the printed list?
DSA C#
class Node {
    public int data;
    public Node next;
    public Node prev;
    public Node(int d) { data = d; next = null; prev = null; }
}

class DoublyLinkedList {
    public Node head;

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

    public void DeleteFromEnd() {
        if (head == null) return;
        if (head.next == null) {
            head = null;
            return;
        }
        Node temp = head;
        while (temp.next != null) {
            temp = temp.next;
        }
        temp.prev.next = null;
    }

    public void PrintList() {
        Node temp = head;
        while (temp != null) {
            System.Console.Write(temp.data);
            if (temp.next != null) System.Console.Write(" <-> ");
            temp = temp.next;
        }
        System.Console.WriteLine();
    }
}

var dll = new DoublyLinkedList();
dll.Append(10);
dll.Append(20);
dll.Append(30);
dll.Append(40);
dll.DeleteFromEnd();
dll.PrintList();
A10 <-> 20 <-> 30 <-> 40
B20 <-> 30 <-> 40
C10 <-> 20 <-> 30
D10 <-> 20
Attempts:
2 left
💡 Hint
Deleting from end removes the last node and updates the previous node's next pointer.
❓ Predict Output
intermediate
2:00remaining
What happens when deleting from an empty doubly linked list?
Given an empty doubly linked list, what is the output after calling DeleteFromEnd() and then printing the list?
DSA C#
class Node {
    public int data;
    public Node next;
    public Node prev;
    public Node(int d) { data = d; next = null; prev = null; }
}

class DoublyLinkedList {
    public Node head;

    public void DeleteFromEnd() {
        if (head == null) return;
        if (head.next == null) {
            head = null;
            return;
        }
        Node temp = head;
        while (temp.next != null) {
            temp = temp.next;
        }
        temp.prev.next = null;
    }

    public void PrintList() {
        Node temp = head;
        if (temp == null) {
            System.Console.WriteLine("List is empty");
            return;
        }
        while (temp != null) {
            System.Console.Write(temp.data);
            if (temp.next != null) System.Console.Write(" <-> ");
            temp = temp.next;
        }
        System.Console.WriteLine();
    }
}

var dll = new DoublyLinkedList();
dll.DeleteFromEnd();
dll.PrintList();
AList is empty
Bnull
C0
DThrows NullReferenceException
Attempts:
2 left
💡 Hint
Check if the list is empty before deleting.
🔧 Debug
advanced
2:00remaining
Identify the bug in DeleteFromEnd method
What is the bug in the following DeleteFromEnd method of a doubly linked list?
DSA C#
public void DeleteFromEnd() {
    if (head == null) return;
    if (head.next == null) {
        head = null;
        return;
    }
    Node temp = head;
    while (temp.next != null) {
        temp = temp.next;
    }
    temp.next = null;
}
AMissing check for head == null
Btemp.next = null; should be temp.prev.next = null;
CShould set head = null when only one node
DNo bug, code is correct
Attempts:
2 left
💡 Hint
Think about how to remove the last node and update the previous node's next pointer.
🧠 Conceptual
advanced
1:00remaining
What is the time complexity of deleting from the end of a doubly linked list without tail pointer?
If a doubly linked list does not maintain a tail pointer, what is the time complexity of deleting the last node?
AO(n^2) because of nested loops
BO(1) because you can directly access the last node
CO(log n) because of binary search
DO(n) because you must traverse to the last node
Attempts:
2 left
💡 Hint
Without a tail pointer, you must start from head and go to the end.
🚀 Application
expert
3:00remaining
After multiple deletions from end, what is the final list?
Starting with a doubly linked list: 5 <-> 15 <-> 25 <-> 35 <-> 45, after calling DeleteFromEnd() three times, what is the printed list?
DSA C#
class Node {
    public int data;
    public Node next;
    public Node prev;
    public Node(int d) { data = d; next = null; prev = null; }
}

class DoublyLinkedList {
    public Node head;

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

    public void DeleteFromEnd() {
        if (head == null) return;
        if (head.next == null) {
            head = null;
            return;
        }
        Node temp = head;
        while (temp.next != null) {
            temp = temp.next;
        }
        temp.prev.next = null;
    }

    public void PrintList() {
        Node temp = head;
        while (temp != null) {
            System.Console.Write(temp.data);
            if (temp.next != null) System.Console.Write(" <-> ");
            temp = temp.next;
        }
        System.Console.WriteLine();
    }
}

var dll = new DoublyLinkedList();
dll.Append(5);
dll.Append(15);
dll.Append(25);
dll.Append(35);
dll.Append(45);
dll.DeleteFromEnd();
dll.DeleteFromEnd();
dll.DeleteFromEnd();
dll.PrintList();
A5 <-> 15
B5 <-> 15 <-> 25
C25 <-> 35 <-> 45
D5
Attempts:
2 left
💡 Hint
Each deletion removes the last node, so after three deletions, three nodes are removed.

Practice

(1/5)
1. What happens when you delete the last node from a doubly linked list that has multiple nodes?
easy
A. The first node is deleted instead.
B. The head pointer is set to null.
C. The list becomes empty immediately.
D. The second last node becomes the new tail and its next pointer is set to null.

Solution

  1. Step 1: Identify the last node and its previous node

    In a doubly linked list, the last node's previous node is the second last node.
  2. Step 2: Update the second last node's next pointer

    Set the second last node's next pointer to null to remove the last node from the list.
  3. Final Answer:

    The second last node becomes the new tail and its next pointer is set to null. -> Option D
  4. Quick Check:

    Deleting last node updates tail's next to null [OK]
Hint: Last node's previous next pointer must be null after deletion [OK]
Common Mistakes:
  • Setting head to null instead of updating tail
  • Not updating the previous node's next pointer
  • Deleting the first node by mistake
2. Which of the following is the correct C# syntax to delete the last node from a doubly linked list named dll?
easy
A. dll.Head = dll.Head.Next; dll.Head.Previous = null;
B. dll.Tail = dll.Tail.Previous; dll.Tail.Next = null;
C. dll.Tail = null; dll.Head = null;
D. dll.Head.Previous = dll.Tail; dll.Tail.Next = dll.Head;

Solution

  1. Step 1: Move tail pointer to previous node

    Set dll.Tail to dll.Tail.Previous to remove the last node.
  2. Step 2: Set new tail's next pointer to null

    Update dll.Tail.Next to null to mark the end of the list.
  3. Final Answer:

    dll.Tail = dll.Tail.Previous; dll.Tail.Next = null; -> Option B
  4. Quick Check:

    Correct tail update syntax = dll.Tail = dll.Tail.Previous; dll.Tail.Next = null; [OK]
Hint: Tail moves back, next pointer cleared [OK]
Common Mistakes:
  • Updating head instead of tail
  • Not setting tail's next to null
  • Setting both head and tail to null unnecessarily
3. Consider the following C# code that deletes the last node from a doubly linked list. What will be the printed list after deletion?
dll.AddLast(1); dll.AddLast(2); dll.AddLast(3); dll.DeleteFromEnd(); dll.PrintList();
medium
A. 2 -> 3 -> null
B. 1 -> 2 -> 3 -> null
C. 1 -> 2 -> null
D. null

Solution

  1. Step 1: Add nodes 1, 2, 3 to the list

    The list becomes 1 -> 2 -> 3 -> null.
  2. Step 2: Delete the last node (3)

    After deletion, the list becomes 1 -> 2 -> null.
  3. Final Answer:

    1 -> 2 -> null -> Option C
  4. Quick Check:

    Last node removed, list ends at 2 [OK]
Hint: Deleting last removes tail node only [OK]
Common Mistakes:
  • Assuming list remains unchanged
  • Deleting head instead of tail
  • Printing list before deletion
4. The following code attempts to delete the last node from a doubly linked list but causes a runtime error. What is the error?
if (dll.Tail != null) { dll.Tail = dll.Tail.Previous; dll.Tail.Next = null; }
medium
A. NullReferenceException when list has only one node
B. Syntax error due to missing braces
C. The head pointer is not updated
D. The list becomes circular

Solution

  1. Step 1: Check deletion when only one node exists

    If only one node exists, dll.Tail.Previous is null, so dll.Tail becomes null.
  2. Step 2: Accessing dll.Tail.Next causes error

    After setting dll.Tail to null, accessing dll.Tail.Next throws NullReferenceException.
  3. Final Answer:

    NullReferenceException when list has only one node -> Option A
  4. Quick Check:

    Accessing property on null tail causes error [OK]
Hint: Check for null tail before accessing Next [OK]
Common Mistakes:
  • Not handling single-node list case
  • Ignoring null checks before pointer access
  • Assuming head updates automatically
5. You have a doubly linked list with nodes: 10 <-> 20 <-> 30 <-> 40. After deleting the last node twice, what will be the list and the tail node's value?
hard
A. 10 <-> 20 -> null, Tail = 20
B. 10 <-> 20 <-> 30 -> null, Tail = 30
C. 10 -> null, Tail = 10
D. Empty list, Tail = null

Solution

  1. Step 1: Initial list and first deletion

    List is 10 <-> 20 <-> 30 <-> 40. Deleting last node removes 40, list becomes 10 <-> 20 <-> 30.
  2. Step 2: Second deletion removes 30

    After second deletion, list becomes 10 <-> 20, and tail points to node with value 20.
  3. Final Answer:

    10 <-> 20 -> null, Tail = 20 -> Option A
  4. Quick Check:

    Two deletions remove last two nodes [OK]
Hint: Each deletion removes tail, tail moves back [OK]
Common Mistakes:
  • Deleting only one node instead of two
  • Not updating tail after deletions
  • Confusing head and tail pointers
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