Delete Node at Beginning in DSA C# - Time & Space Complexity
We want to understand how long it takes to remove the first item from a linked list.
How does the time needed change when the list grows bigger?
Analyze the time complexity of the following code snippet.
// Remove the first node from a singly linked list
public void DeleteAtBeginning()
{
if (head == null) return;
head = head.Next;
}
This code removes the first node by moving the head pointer to the next node.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Updating the head pointer once.
- How many times: Exactly once, no loops or recursion.
Removing the first node always takes the same steps, no matter how big the list is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The work stays the same even if the list grows.
Time Complexity: O(1)
This means deleting the first node takes a fixed amount of time regardless of list size.
[X] Wrong: "Deleting the first node takes longer if the list is bigger because we have to check all nodes."
[OK] Correct: We only change the head pointer once; we do not look at other nodes.
Knowing that deleting the first node is quick helps you explain efficient list operations clearly and confidently.
"What if we had to delete the last node instead? How would the time complexity change?"
Practice
Solution
Step 1: Understand the head pointer role
The head pointer points to the first node in the list.Step 2: Update head to next node
Deleting the first node means moving the head pointer to the second node, effectively removing the first node.Final Answer:
The head pointer moves to the second node, removing the first node from the list -> Option DQuick Check:
Delete first node = move head to next [OK]
- Trying to delete the last node instead
- Not updating the head pointer
- Deleting the entire list accidentally
Solution
Step 1: Identify the head pointer update
To delete the first node, assign head to head.next to move the head pointer forward.Step 2: Check syntax correctness
head = head.next; correctly updates the head pointer to the second node.Final Answer:
head = head.next; -> Option CQuick Check:
Correct syntax to delete first node = head = head.next; [OK]
- Reversing the assignment order
- Using null.next which is invalid
- Setting head.next to null instead of head
Node head = new Node(1); head.next = new Node(2); head.next.next = new Node(3); head = head.next;What is the state of the list after deletion?
Solution
Step 1: Initial list creation
The list starts as 1 -> 2 -> 3 -> null with head at node 1.Step 2: Delete first node by moving head
head = head.next moves head to node 2, so the list becomes 2 -> 3 -> null.Final Answer:
2 -> 3 -> null -> Option AQuick Check:
After deletion, list starts at second node [OK]
- Forgetting to update head pointer
- Assuming first node still exists
- Confusing node values after deletion
Node head = null; head = head.next;What is the error and how to fix it?
Solution
Step 1: Identify cause of error
head is null, so accessing head.next causes NullReferenceException.Step 2: Fix by adding null check
Check if head != null before assigning head = head.next to avoid error.Final Answer:
NullReferenceException; fix by checking if head is not null before deleting -> Option BQuick Check:
Null head causes error; check before delete [OK]
- Ignoring null checks causing runtime errors
- Assuming list always has nodes
- Confusing syntax errors with runtime errors
head = head.next; two times. What is the resulting list?Solution
Step 1: Initial list and first deletion
List starts as 5 -> 10 -> 15 -> 20 -> null. After first deletion, head moves to 10, list is 10 -> 15 -> 20 -> null.Step 2: Second deletion moves head again
After second deletion, head moves to 15, list is 15 -> 20 -> null.Final Answer:
15 -> 20 -> null -> Option AQuick Check:
Two deletions remove first two nodes [OK]
- Deleting only once instead of twice
- Confusing node values after multiple deletions
- Assuming list becomes empty too early
Ready to prove it?
Pass the test and this pattern moves unknown
