Bird
Raised Fist0
DSA C#programming~10 mins

Insert at Beginning of Doubly Linked List in DSA C# - Execution Trace

Choose your learning style10 modes available
Concept Flow - Insert at Beginning of Doubly Linked List
Create new node
↓
Set newNode.next = head
↓
If head != null
↓
Set head.prev = newNode
↓
Set head = newNode
↓
Done
This flow shows how a new node is created and inserted at the start of the doubly linked list by adjusting next and prev pointers.
Execution Sample
DSA C#
class Node {
  public int data;
  public Node prev, next;
  public Node(int d) { data = d; prev = next = null; }
}

// Insert at beginning
void InsertAtBeginning(ref Node head, int data) {
  Node newNode = new Node(data);
  newNode.next = head;
  if (head != null) {
    head.prev = newNode;
  }
  head = newNode;
}
This code creates a new node and inserts it at the beginning of a doubly linked list.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Start with empty listāˆ…head = nullList is empty
2Create new node with data=10Node(10)newNode.prev = null, newNode.next = nullā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ prev:āˆ… │ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜
3Set newNode.next = head (null)Node(10)newNode.next = nullā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ prev:āˆ… │ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜
4Since head is null, skip head.prev updateNode(10)No changeā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ prev:āˆ… │ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜
5Set head = newNodeNode(10)head → newNodehead ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ prev:āˆ… │ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜
6Insert another node with data=20 at beginningNode(20) → Node(10)newNode.prev = null, newNode.next = head head.prev = newNode head = newNodeBefore: head ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ │ prev:āˆ… │ │ prev:āˆ… │ │ next:āˆ… │ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ Step 1: newNode.next = head Step 2: head.prev = newNode Step 3: head = newNode After: head ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:20│ │ data:10│ │ prev:āˆ… │←── │ prev:20│ │ next:──┼──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜
7Insert node with data=30 at beginningNode(30) → Node(20) → Node(10)newNode.prev = null, newNode.next = head head.prev = newNode head = newNodeBefore: head ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:20│ │ data:10│ │ data:30│ │ prev:āˆ… │←── │ prev:20│ │ prev:āˆ… │ │ next:──┼──→ │ next:āˆ… │ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ Step 1: newNode.next = head Step 2: head.prev = newNode Step 3: head = newNode After: head ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:30│ │ data:20│ │ data:10│ │ prev:āˆ… │←── │ prev:30│←── │ prev:20│ │ next:──┼──→ │ next:──┼──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜
8End of insertionsNode(30) → Node(20) → Node(10)head points to Node(30)Final list: head ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:30│ │ data:20│ │ data:10│ │ prev:āˆ… │←── │ prev:30│←── │ prev:20│ │ next:──┼──→ │ next:──┼──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜
šŸ’” No more nodes to insert; insertion at beginning complete.
Variable Tracker
VariableStartAfter Step 2After Step 6After Step 7Final
headnullNode(10)Node(20)Node(30)Node(30)
newNodenullNode(10)Node(20)Node(30)null (reset after insertion)
newNode.prevnullnullnullnullnull
newNode.nextnullnullNode(10)Node(20)Node(20)
Key Moments - 3 Insights
Why do we set newNode.next = head before changing head?
Because we need newNode to point to the current first node before head changes. See execution_table step 6 where newNode.next is set to old head before head is updated.
Why do we check if head is null before setting head.prev?
If head is null (empty list), there is no node to update prev pointer. This avoids errors. See execution_table step 4 where this check skips updating head.prev.
How do prev pointers help in a doubly linked list?
Prev pointers allow backward traversal. After insertion, newNode.prev is null (start), and old head.prev points back to newNode. See pointer changes in steps 6 and 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 6, what does newNode.next point to after insertion?
ANode with data 10
BNode with data 20
Cnull
DNode with data 30
šŸ’” Hint
Check the 'Pointer Changes' and 'Visual State' columns at step 6 showing newNode.next = head which was Node(10)
At which step does the list first have two nodes linked?
AStep 3
BStep 5
CStep 6
DStep 7
šŸ’” Hint
Look at 'Nodes in List' column to see when list length changes from 1 to 2
If head was not null, what pointer must be updated to maintain the doubly linked list?
AnewNode.next
Bhead.prev
CnewNode.prev
DnewNode.data
šŸ’” Hint
Refer to concept_flow and execution_table step 6 where head.prev is updated to newNode
Concept Snapshot
Insert at Beginning of Doubly Linked List:
- Create new node with data
- Set newNode.next to current head
- If head exists, set head.prev to newNode
- Update head to newNode
- newNode.prev is always null (start of list)
- Maintains forward and backward links
Full Transcript
To insert a node at the beginning of a doubly linked list, first create a new node with the given data. Then set the new node's next pointer to the current head of the list. If the list is not empty, update the current head's prev pointer to point back to the new node. Finally, update the head pointer to the new node. This ensures the new node is now the first node, with its prev pointer null and next pointing to the old first node. The doubly linked list maintains both forward and backward links after insertion.

Practice

(1/5)
1. What is the main purpose of inserting a node at the beginning of a doubly linked list?
easy
A. To add a new node as the first element of the list
B. To remove the last node from the list
C. To search for a node in the list
D. To reverse the entire list

Solution

  1. Step 1: Understand the operation

    Inserting at the beginning means adding a new node before the current first node.
  2. Step 2: Identify the effect on the list

    The new node becomes the head (first node), shifting others to the right.
  3. Final Answer:

    To add a new node as the first element of the list -> Option A
  4. Quick Check:

    Insert at beginning = Add first node [OK]
Hint: Insert at beginning means new node becomes the first [OK]
Common Mistakes:
  • Confusing insertion with deletion
  • Thinking insertion adds at the end
  • Mixing up search with insertion
2. Which of the following is the correct syntax to create a new node in C# for a doubly linked list?
easy
A. Node newNode = new Node(data);
B. newNode = Node(data);
C. Node newNode = Node(data);
D. Node newNode = new Node[];

Solution

  1. Step 1: Recall C# object creation syntax

    In C#, objects are created using the 'new' keyword followed by the constructor call.
  2. Step 2: Match syntax with node creation

    Correct syntax is 'Node newNode = new Node(data);' to create a new node with data.
  3. Final Answer:

    Node newNode = new Node(data); -> Option A
  4. Quick Check:

    C# new object = new ClassName() [OK]
Hint: Use 'new' keyword to create objects in C# [OK]
Common Mistakes:
  • Omitting 'new' keyword
  • Using array syntax for single node
  • Missing type declaration
3. Consider this code snippet inserting a node with value 10 at the beginning of a doubly linked list:
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?
medium
A. 30 <-> 20 <-> 10 <-> null
B. 20 <-> 30 <-> 10 <-> null
C. 10 <-> 30 <-> 20 <-> null
D. 10 <-> 20 <-> 30 <-> null

Solution

  1. 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.
  2. Step 2: Update head pointer

    Head now points to new node (10), so list starts with 10.
  3. Final Answer:

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

    New node at front = 10 first [OK]
Hint: New node points to old head; update old head's prev [OK]
Common Mistakes:
  • Not updating old head's prev pointer
  • Appending new node at end instead of beginning
  • Not updating head to new node
4. Identify the error in this code snippet for inserting at the beginning of a doubly linked list:
Node newNode = new Node(5);
newNode.next = head;
head.prev = newNode;
head = newNode;
medium
A. Incorrect assignment of newNode.next
B. Missing check if head is null before accessing head.prev
C. head should be assigned before setting newNode.next
D. newNode.prev should be set to head

Solution

  1. Step 1: Analyze head.prev assignment

    Code sets head.prev = newNode without checking if head is null, causing error if list is empty.
  2. Step 2: Identify missing null check

    Must check if head != null before setting head.prev to avoid null reference error.
  3. Final Answer:

    Missing check if head is null before accessing head.prev -> Option B
  4. Quick Check:

    Check head != null before head.prev [OK]
Hint: Always check head != null before accessing head.prev [OK]
Common Mistakes:
  • Ignoring null check on head
  • Setting pointers in wrong order
  • Confusing prev and next pointers
5. You have a doubly linked list with nodes: 5 <-> 15 <-> 25 <-> null. You want to insert a new node with value 1 at the beginning. After insertion, which of the following correctly represents the list and the pointers of the new node?
hard
A. 5 <-> 15 <-> 25 <-> 1 <-> null, newNode.prev = 25, newNode.next = null
B. 1 <-> 5 <-> 15 <-> 25 <-> null, newNode.prev = 5, newNode.next = null
C. 1 <-> 5 <-> 15 <-> 25 <-> null, newNode.prev = null, newNode.next = 5
D. 5 <-> 15 <-> 25 <-> 1 <-> null, newNode.prev = null, newNode.next = 5

Solution

  1. Step 1: Insert new node at beginning

    New node with value 1 becomes head, points next to old head (5), prev is null.
  2. Step 2: Confirm pointers and list order

    List order is 1 <-> 5 <-> 15 <-> 25 <-> null, newNode.prev = null, newNode.next = 5.
  3. Final Answer:

    1 <-> 5 <-> 15 <-> 25 <-> null, newNode.prev = null, newNode.next = 5 -> Option C
  4. Quick Check:

    New head prev=null, next=old head [OK]
Hint: New head prev=null, next=old head node [OK]
Common Mistakes:
  • Setting newNode.prev to old head
  • Appending new node at end instead of beginning
  • Not updating head pointer
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