Bird
Raised Fist0
DSA C#programming~20 mins

Delete Node at End in DSA C# - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Delete Node at End Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output after deleting the last node in a singly linked list
What is the printed state of the linked list after deleting the last node?
DSA C#
public class Node {
    public int data;
    public Node next;
    public Node(int d) { data = d; next = null; }
}

public class LinkedList {
    public Node head;

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

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

class Program {
    static void Main() {
        LinkedList list = new LinkedList();
        list.head = new Node(10);
        list.head.next = new Node(20);
        list.head.next.next = new Node(30);
        list.DeleteAtEnd();
        list.PrintList();
    }
}
A10 -> 20 -> 30 -> null
B10 -> null
C20 -> 30 -> null
D10 -> 20 -> null
Attempts:
2 left
💡 Hint
Think about what happens to the second last node's next pointer after deletion.
❓ Predict Output
intermediate
2:00remaining
Output after deleting the last node in a single-node list
What is the printed state of the linked list after deleting the last node when the list has only one node?
DSA C#
public class Node {
    public int data;
    public Node next;
    public Node(int d) { data = d; next = null; }
}

public class LinkedList {
    public Node head;

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

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

class Program {
    static void Main() {
        LinkedList list = new LinkedList();
        list.head = new Node(100);
        list.DeleteAtEnd();
        list.PrintList();
    }
}
Anull
BError: NullReferenceException
C100
D100 -> null
Attempts:
2 left
💡 Hint
Deleting the only node should leave the list empty.
🔧 Debug
advanced
2:00remaining
Identify the error in deleting the last node
What error will this code produce when deleting the last node?
DSA C#
public void DeleteAtEnd() {
    if (head == null) return;
    Node temp = head;
    while (temp.next != null) {
        temp = temp.next;
    }
    temp = null;
}
ANo error, deletes last node correctly
BNullReferenceException at runtime
CThe last node is not deleted; list remains unchanged
DCompilation error due to invalid assignment
Attempts:
2 left
💡 Hint
Setting temp to null does not change the linked list structure.
🧠 Conceptual
advanced
1:00remaining
Time complexity of deleting the last node in singly linked list
What is the time complexity of deleting the last node in a singly linked list of n nodes?
AO(1) - constant time
BO(n) - linear time
CO(log n) - logarithmic time
DO(n^2) - quadratic time
Attempts:
2 left
💡 Hint
You must traverse the list to find the second last node.
❓ Predict Output
expert
3:00remaining
Output after multiple deletions at end
What is the printed state of the linked list after deleting the last node three times in a row?
DSA C#
public class Node {
    public int data;
    public Node next;
    public Node(int d) { data = d; next = null; }
}

public class LinkedList {
    public Node head;

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

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

class Program {
    static void Main() {
        LinkedList list = new LinkedList();
        list.head = new Node(1);
        list.head.next = new Node(2);
        list.head.next.next = new Node(3);
        list.head.next.next.next = new Node(4);
        list.DeleteAtEnd();
        list.DeleteAtEnd();
        list.DeleteAtEnd();
        list.PrintList();
    }
}
A1 -> null
Bnull
C1 -> 2 -> null
D2 -> 3 -> null
Attempts:
2 left
💡 Hint
Each deletion removes the last node, so the list shrinks from the end.

Practice

(1/5)
1. What does the operation Delete Node at End do in a singly linked list?
easy
A. Adds a new node at the end of the list
B. Removes the first node from the list
C. Removes the last node from the list
D. Adds a new node at the beginning of the list

Solution

  1. Step 1: Understand the operation meaning

    Deleting a node at the end means removing the last node in the linked list.
  2. Step 2: Compare options with operation

    Only Removes the last node from the list matches removing the last node; others add or remove nodes elsewhere.
  3. Final Answer:

    Removes the last node from the list -> Option C
  4. Quick Check:

    Delete Node at End = Remove last node [OK]
Hint: Deleting at end means removing the last node [OK]
Common Mistakes:
  • Confusing delete at end with delete at beginning
  • Thinking it adds a node instead of removing
  • Assuming it deletes a middle node
2. Which of the following is the correct syntax to delete the last node in a singly linked list in C#?
easy
A. Create a new node and assign it to last node
B. Set head to null directly
C. Set last node's next to head
D. Traverse to second last node, set its next to null

Solution

  1. Step 1: Identify correct deletion method

    To delete last node, we must reach second last node and set its next pointer to null.
  2. Step 2: Check options for this logic

    Traverse to second last node, set its next to null matches this logic; others do unrelated actions.
  3. Final Answer:

    Traverse to second last node, set its next to null -> Option D
  4. Quick Check:

    Delete last node = second last node next = null [OK]
Hint: Set second last node's next to null to delete last [OK]
Common Mistakes:
  • Setting head to null deletes whole list
  • Trying to set last node's next to head causes cycle
  • Creating new node does not delete existing node
3. Given the linked list: 10 -> 20 -> 30 -> 40 -> null, what will be the list after deleting the node at the end?
medium
A. 10 -> 20 -> 30 -> 40 -> null
B. 10 -> 20 -> 30 -> null
C. 20 -> 30 -> 40 -> null
D. 10 -> 20 -> null

Solution

  1. Step 1: Identify last node in list

    The last node is 40 in the list 10 -> 20 -> 30 -> 40 -> null.
  2. Step 2: Remove last node and update list

    Deleting last node removes 40, so list becomes 10 -> 20 -> 30 -> null.
  3. Final Answer:

    10 -> 20 -> 30 -> null -> Option B
  4. Quick Check:

    Last node 40 removed = 10->20->30->null [OK]
Hint: Remove last value to get new list [OK]
Common Mistakes:
  • Not removing last node, list unchanged
  • Removing first node instead
  • Removing more than one node accidentally
4. Consider this code snippet to delete the last node of a singly linked list:
Node current = head;
while(current.next.next != null) {
    current = current.next;
}
current.next = null;
What error can occur when this code runs on a list with only one node?
medium
A. NullReferenceException because current.next.next is invalid
B. No error, code works fine
C. Infinite loop because condition never false
D. Deletes the head node incorrectly

Solution

  1. Step 1: Analyze condition for single node list

    If list has one node, current.next is null, so current.next.next causes error.
  2. Step 2: Identify exception type

    Accessing next of null causes NullReferenceException in C#.
  3. Final Answer:

    NullReferenceException because current.next.next is invalid -> Option A
  4. Quick Check:

    Single node list causes null.next access error [OK]
Hint: Check for null before accessing next.next [OK]
Common Mistakes:
  • Ignoring null check for single node
  • Assuming loop runs safely always
  • Thinking it deletes head node
5. You have a singly linked list with nodes: 5 -> 15 -> 25 -> 35 -> 45 -> null. Write a method to delete the last node and return the updated list. Which of the following code snippets correctly implements this?
hard
A. if (head == null) return null; if (head.next == null) return null; Node current = head; while (current.next.next != null) { current = current.next; } current.next = null; return head;
B. if (head == null) return null; Node current = head; while (current.next != null) { current = current.next; } current = null; return head;
C. if (head == null) return null; Node current = head; while (current.next.next != null) { current = current.next; } current.next = current; return head;
D. if (head == null) return null; head = head.next; return head;

Solution

  1. Step 1: Handle empty and single node cases

    If list is empty or has one node, deleting last node returns null (empty list).
  2. Step 2: Traverse to second last node and delete last

    Loop until current.next.next is null, then set current.next to null to remove last node.
  3. Step 3: Return updated head

    Return head pointer to updated list.
  4. Final Answer:

    Code snippet D correctly deletes last node and returns updated list -> Option A
  5. Quick Check:

    Check null, traverse to second last, set next null [OK]
Hint: Check list size, then remove last by second last next = null [OK]
Common Mistakes:
  • Setting current to null does not remove last node
  • Assigning current.next to current creates cycle
  • Deleting head instead of last node
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