Bird
Raised Fist0
DSA C#programming~20 mins

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

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
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 node at position 2 in a singly linked list
What is the printed state of the linked list after deleting the node at position 2?
DSA C#
using System;

class Node {
    public int data;
    public Node next;
    public Node(int d) { data = d; next = null; }
}

class LinkedList {
    public Node head;

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

    public void PrintList() {
        Node temp = head;
        while (temp != null) {
            Console.Write(temp.data + " -> ");
            temp = temp.next;
        }
        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.head.next.next.next = new Node(40);
        list.DeleteNodeAtPosition(2);
        list.PrintList();
    }
}
A10 -> 20 -> 40 -> null
B10 -> 30 -> 40 -> null
C20 -> 30 -> 40 -> null
D10 -> 20 -> 30 -> null
Attempts:
2 left
💡 Hint
Remember that position 0 is the head node. Deleting at position 2 removes the third node.
❓ Predict Output
intermediate
2:00remaining
Output after deleting node at position 0 (head) in a singly linked list
What is the printed state of the linked list after deleting the node at position 0?
DSA C#
using System;

class Node {
    public int data;
    public Node next;
    public Node(int d) { data = d; next = null; }
}

class LinkedList {
    public Node head;

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

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

class Program {
    static void Main() {
        LinkedList list = new LinkedList();
        list.head = new Node(5);
        list.head.next = new Node(15);
        list.head.next.next = new Node(25);
        list.DeleteNodeAtPosition(0);
        list.PrintList();
    }
}
A5 -> 25 -> null
B15 -> 25 -> null
C5 -> 15 -> 25 -> null
Dnull
Attempts:
2 left
💡 Hint
Deleting at position 0 removes the head node.
🔧 Debug
advanced
2:00remaining
Identify the error when deleting a node at an invalid position
What error or behavior occurs when trying to delete a node at position 5 in a list of 3 nodes?
DSA C#
using System;

class Node {
    public int data;
    public Node next;
    public Node(int d) { data = d; next = null; }
}

class LinkedList {
    public Node head;

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

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.DeleteNodeAtPosition(5);
        Console.WriteLine("Deletion attempted at position 5");
    }
}
ANullReferenceException at runtime
BCompilation error due to invalid loop
CIndexOutOfRangeException at runtime
DNo error, list remains unchanged
Attempts:
2 left
💡 Hint
Check the conditions that stop deletion if position is invalid.
❓ Predict Output
advanced
2:00remaining
Output after deleting last node in a singly linked list
What is the printed state of the linked list after deleting the node at the last position?
DSA C#
using System;

class Node {
    public int data;
    public Node next;
    public Node(int d) { data = d; next = null; }
}

class LinkedList {
    public Node head;

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

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

class Program {
    static void Main() {
        LinkedList list = new LinkedList();
        list.head = new Node(7);
        list.head.next = new Node(14);
        list.head.next.next = new Node(21);
        list.head.next.next.next = new Node(28);
        list.DeleteNodeAtPosition(3);
        list.PrintList();
    }
}
A7 -> 14 -> 28 -> null
B7 -> 14 -> 21 -> 28 -> null
C7 -> 14 -> 21 -> null
D14 -> 21 -> 28 -> null
Attempts:
2 left
💡 Hint
Deleting at position 3 removes the last node in a 4-node list.
🧠 Conceptual
expert
2:00remaining
Effect of deleting a node at a position greater than list length
If you try to delete a node at a position greater than the length of the singly linked list, what is the expected behavior of the given DeleteNodeAtPosition method?
DSA C#
public void DeleteNodeAtPosition(int position) {
    if (head == null) return;
    Node temp = head;
    if (position == 0) {
        head = temp.next;
        return;
    }
    for (int i = 0; temp != null && i < position - 1; i++) {
        temp = temp.next;
    }
    if (temp == null || temp.next == null) return;
    temp.next = temp.next.next;
}
AThe method does nothing and the list remains unchanged
BThe method deletes the last node in the list
CThe method throws a NullReferenceException at runtime
DThe method deletes the head node
Attempts:
2 left
💡 Hint
Look at the checks for temp and temp.next before deletion.

Practice

(1/5)
1. What does deleting a node at a specific position in a singly linked list mean?
easy
A. Adding a new node at that position
B. Removing the node at that position and linking its previous node to the next node
C. Swapping the node at that position with the last node
D. Reversing the entire list up to that position

Solution

  1. Step 1: Understand node deletion in linked list

    Deleting a node means removing it and connecting the previous node to the next node to keep the list intact.
  2. Step 2: Identify the effect of deletion at a specific position

    The node at the given position is removed, and the previous node's next pointer is updated to skip the deleted node.
  3. Final Answer:

    Removing the node at that position and linking its previous node to the next node -> Option B
  4. Quick Check:

    Delete node = Remove and link previous to next [OK]
Hint: Delete means remove node and link previous to next [OK]
Common Mistakes:
  • Thinking deletion adds a node
  • Confusing deletion with swapping nodes
  • Assuming deletion reverses the list
2. Which of the following is the correct C# syntax to delete a node at position pos in a singly linked list starting with head?
easy
A. head.next = null;
B. head = head.next[pos];
C. if (pos == 0) head = head.next;
D. head.deleteNode(pos);

Solution

  1. Step 1: Check deletion at head position

    If position is 0, deleting means moving head to next node: head = head.next;.
  2. Step 2: Validate other options

    head.next = null;: head.next = null; deletes second node regardless of pos. head = head.next[pos];: head = head.next[pos]; invalid syntax (no array indexing on nodes). head.deleteNode(pos);: head.deleteNode(pos); calls non-existent method.
  3. Final Answer:

    if (pos == 0) head = head.next; -> Option C
  4. Quick Check:

    Delete head = head.next [OK]
Hint: To delete head, assign head to head.next [OK]
Common Mistakes:
  • Using array-like indexing on linked list
  • Calling undefined methods
  • Setting next to null incorrectly
3. What will be the linked list after deleting the node at position 2 from the list: 1 -> 3 -> 5 -> 7 -> null?
medium
A. 1 -> 3 -> 7 -> null
B. 1 -> 5 -> 7 -> null
C. 3 -> 5 -> 7 -> null
D. 1 -> 3 -> 5 -> null

Solution

  1. Step 1: Identify node at position 2

    Positions start at 0: 0->1, 1->3, 2->5, 3->7. Node at position 2 is 5.
  2. Step 2: Delete node 5 and link previous to next

    Remove 5, link node with value 3 to node with value 7.
  3. Final Answer:

    1 -> 3 -> 7 -> null -> Option A
  4. Quick Check:

    Delete node 5 at pos 2 = 1->3->7 [OK]
Hint: Count from zero, remove node at position, link neighbors [OK]
Common Mistakes:
  • Deleting wrong node by counting from 1
  • Not linking previous node to next
  • Removing head instead of position 2
4. Given the following code snippet to delete a node at position pos, what is the error?
Node current = head;
for (int i = 0; i < pos - 1; i++) {
    current = current.next;
}
current.next = current.next.next;
medium
A. It does not handle deletion at position 0
B. It causes a syntax error due to missing semicolons
C. It deletes the node after the given position
D. It causes infinite loop

Solution

  1. Step 1: Analyze loop for position 0

    For pos=0, loop condition i < pos - 1 (i < -1) is false, current remains head, then current.next = current.next.next; deletes SECOND node (pos 1) instead of head.
  2. Step 2: Check deletion logic for other positions

    For pos >=1, loop moves current to node before target and skips target correctly.
  3. Final Answer:

    It does not handle deletion at position 0 -> Option A
  4. Quick Check:

    Code misses head deletion case [OK]
Hint: Check if code handles deleting head (pos 0) separately [OK]
Common Mistakes:
  • Ignoring head deletion case
  • Assuming loop runs for pos=0
  • Confusing syntax errors with logic errors
5. You have a singly linked list: 10 -> 20 -> 30 -> 40 -> 50 -> null. You want to delete the node at position 4. After deletion, what will be the list? Also, what happens if you try to delete at position 5?
hard
A. 10 -> 20 -> 30 -> 40 -> 50 -> null; Deleting at position 5 deletes head
B. 10 -> 20 -> 30 -> 40 -> null; Deleting at position 5 causes no change (position out of range)
C. 10 -> 20 -> 30 -> 40 -> 50 -> null; Deleting at position 5 deletes last node
D. 10 -> 20 -> 30 -> 40 -> null; Deleting at position 5 causes runtime error (null reference)

Solution

  1. Step 1: Delete node at position 4

    Positions: 0->10,1->20,2->30,3->40,4->50. Deleting node 50 removes last node, list becomes 10->20->30->40->null.
  2. Step 2: Attempt deletion at position 5

    Position 5 does not exist. Trying to access node at pos 5 causes null reference error (runtime error).
  3. Final Answer:

    10 -> 20 -> 30 -> 40 -> null; Deleting at position 5 causes runtime error (null reference) -> Option D
  4. Quick Check:

    Delete last node works; out-of-range causes error [OK]
Hint: Deleting beyond list length causes runtime error [OK]
Common Mistakes:
  • Assuming out-of-range deletion does nothing
  • Deleting wrong node at position 5
  • Confusing last node position with list length
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