Insert at Beginning of Doubly Linked List in DSA C# - Time & Space Complexity
We want to understand how the time needed to insert a node at the start of a doubly linked list changes as the list grows.
How does the number of steps grow when the list gets bigger?
Analyze the time complexity of the following code snippet.
public void InsertAtBeginning(int data) {
Node newNode = new Node(data);
newNode.Next = Head;
if (Head != null) {
Head.Prev = newNode;
}
Head = newNode;
}
This code adds a new node at the start of a doubly linked list by adjusting pointers.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Creating and linking one new node.
- How many times: Exactly once per insertion, no loops or recursion.
Each insertion does the same fixed steps regardless of list size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 4 steps |
| 100 | 4 steps |
| 1000 | 4 steps |
Pattern observation: The number of steps stays the same no matter how big the list is.
Time Complexity: O(1)
This means inserting at the beginning takes a constant amount of time, no matter how large the list is.
[X] Wrong: "Inserting at the start takes longer as the list grows because we have to move all nodes."
[OK] Correct: We only change a few pointers at the start; we do not move or touch other nodes.
Knowing that insertion at the start of a doubly linked list is quick helps you explain efficient data structure operations clearly in interviews.
"What if we changed this to insert at the end of the doubly linked list without a tail pointer? How would the time complexity change?"
Practice
Solution
Step 1: Understand the operation
Inserting at the beginning means adding a new node before the current first node.Step 2: Identify the effect on the list
The new node becomes the head (first node), shifting others to the right.Final Answer:
To add a new node as the first element of the list -> Option AQuick Check:
Insert at beginning = Add first node [OK]
- Confusing insertion with deletion
- Thinking insertion adds at the end
- Mixing up search with insertion
Solution
Step 1: Recall C# object creation syntax
In C#, objects are created using the 'new' keyword followed by the constructor call.Step 2: Match syntax with node creation
Correct syntax is 'Node newNode = new Node(data);' to create a new node with data.Final Answer:
Node newNode = new Node(data); -> Option AQuick Check:
C# new object = new ClassName() [OK]
- Omitting 'new' keyword
- Using array syntax for single node
- Missing type declaration
Node newNode = new Node(10);
newNode.next = head;
if (head != null) {
head.prev = newNode;
}
head = newNode;
What will be the printed list after insertion if the original list was: 20 <-> 30 <-> null?Solution
Step 1: Insert new node at beginning
New node with 10 points next to old head (20). Old head's prev points back to new node.Step 2: Update head pointer
Head now points to new node (10), so list starts with 10.Final Answer:
10 <-> 20 <-> 30 <-> null -> Option DQuick Check:
New node at front = 10 first [OK]
- Not updating old head's prev pointer
- Appending new node at end instead of beginning
- Not updating head to new node
Node newNode = new Node(5); newNode.next = head; head.prev = newNode; head = newNode;
Solution
Step 1: Analyze head.prev assignment
Code sets head.prev = newNode without checking if head is null, causing error if list is empty.Step 2: Identify missing null check
Must check if head != null before setting head.prev to avoid null reference error.Final Answer:
Missing check if head is null before accessing head.prev -> Option BQuick Check:
Check head != null before head.prev [OK]
- Ignoring null check on head
- Setting pointers in wrong order
- Confusing prev and next pointers
Solution
Step 1: Insert new node at beginning
New node with value 1 becomes head, points next to old head (5), prev is null.Step 2: Confirm pointers and list order
List order is 1 <-> 5 <-> 15 <-> 25 <-> null, newNode.prev = null, newNode.next = 5.Final Answer:
1 <-> 5 <-> 15 <-> 25 <-> null, newNode.prev = null, newNode.next = 5 -> Option CQuick Check:
New head prev=null, next=old head [OK]
- Setting newNode.prev to old head
- Appending new node at end instead of beginning
- Not updating head pointer
Ready to prove it?
Pass the test and this pattern moves unknown
