What if you could add new items instantly at the start without moving everything else?
Why Insert at Beginning of Doubly Linked List in DSA C#?
Imagine you have a paper list of names, and you want to add a new name at the very top every time someone new joins. Doing this by rewriting the whole list each time is tiring and slow.
Manually moving all names down to add a new one at the top takes a lot of time and can cause mistakes like losing or mixing names. It's hard to keep track when the list grows long.
A doubly linked list lets you add a new item at the start quickly by just changing a few links, without moving the whole list. It keeps track of neighbors both ways, making insertions smooth and safe.
string[] names = new string[100]; // Shift all names down by one for (int i = count; i > 0; i--) { names[i] = names[i - 1]; } names[0] = newName; count++;
Node newNode = new Node(newName);
newNode.Next = head;
if (head != null) {
head.Prev = newNode;
}
head = newNode;You can quickly add new items at the start of a list without slowing down as the list grows.
Think of a music playlist where you want to add a new favorite song to the top instantly without rearranging the whole list.
Manual shifting of items is slow and error-prone.
Doubly linked lists allow fast insertion at the beginning by adjusting links.
This method keeps the list organized and efficient as it grows.
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
