Bird
Raised Fist0
DSA C#programming~20 mins

Insert at Beginning Head Insert in DSA C# - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Head Insert Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output after inserting nodes at the beginning
What is the printed state of the linked list after inserting the values 10, 20, and 30 at the beginning in that order?
DSA C#
public class Node {
    public int data;
    public Node next;
    public Node(int d) { data = d; next = null; }
}

public class LinkedList {
    public Node head;

    public void InsertAtBeginning(int newData) {
        Node newNode = new Node(newData);
        newNode.next = head;
        head = newNode;
    }

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

LinkedList list = new LinkedList();
list.InsertAtBeginning(10);
list.InsertAtBeginning(20);
list.InsertAtBeginning(30);
list.PrintList();
A30 -> 20 -> 10 -> null
B10 -> 20 -> 30 -> null
Cnull -> 30 -> 20 -> 10
D20 -> 30 -> 10 -> null
Attempts:
2 left
💡 Hint
Remember that inserting at the beginning means the newest node becomes the head.
❓ Predict Output
intermediate
2:00remaining
Resulting list after multiple head inserts
What will be the output after inserting 5, then 15, then 25 at the beginning of an empty linked list and printing it?
DSA C#
LinkedList list = new LinkedList();
list.InsertAtBeginning(5);
list.InsertAtBeginning(15);
list.InsertAtBeginning(25);
list.PrintList();
Anull
B5 -> 15 -> 25 -> null
C15 -> 25 -> 5 -> null
D25 -> 15 -> 5 -> null
Attempts:
2 left
💡 Hint
The last inserted node is always the new head.
🔧 Debug
advanced
2:00remaining
Identify the bug in head insertion
What error will occur when running this code that tries to insert a node at the beginning of a linked list?
DSA C#
public void InsertAtBeginning(int newData) {
    Node newNode = new Node(newData);
    newNode.next = head.next;
    head = newNode;
}
AStackOverflowException
BNullReferenceException
CNo error, works correctly
DCompile-time error
Attempts:
2 left
💡 Hint
Check what happens if head is null before accessing head.next.
🧠 Conceptual
advanced
2:00remaining
Effect of head insertion on list order
If you insert elements 1, 2, 3 in that order at the beginning of an empty linked list, what will be the order of elements when traversed from head?
A1 -> 3 -> 2 -> null
B1 -> 2 -> 3 -> null
C3 -> 2 -> 1 -> null
D2 -> 3 -> 1 -> null
Attempts:
2 left
💡 Hint
Each new node becomes the new head.
🚀 Application
expert
3:00remaining
Final list after mixed insertions
Given an empty linked list, you perform these operations in order: 1. Insert 100 at beginning 2. Insert 200 at beginning 3. Insert 300 at beginning 4. Insert 400 at beginning What is the final printed list?
DSA C#
LinkedList list = new LinkedList();
list.InsertAtBeginning(100);
list.InsertAtBeginning(200);
list.InsertAtBeginning(300);
list.InsertAtBeginning(400);
list.PrintList();
A400 -> 300 -> 200 -> 100 -> null
B400 -> 200 -> 300 -> 100 -> null
C100 -> 300 -> 200 -> 400 -> null
D100 -> 200 -> 300 -> 400 -> null
Attempts:
2 left
💡 Hint
Remember the newest inserted node is always the head.

Practice

(1/5)
1. What does the "Insert at Beginning" operation do in a singly linked list?
easy
A. Deletes the last node of the list
B. Adds a new node at the start of the list
C. Deletes the first node of the list
D. Adds a new node at the end of the list

Solution

  1. Step 1: Understand the operation

    Inserting at the beginning means placing a new node before the current first node.
  2. Step 2: Effect on the list

    The new node becomes the head, pushing existing nodes after it.
  3. Final Answer:

    Adds a new node at the start of the list -> Option B
  4. Quick Check:

    Insert at beginning = Add new head [OK]
Hint: New node becomes head, old head moves next [OK]
Common Mistakes:
  • Confusing insert at beginning with insert at end
  • Thinking it deletes nodes
  • Assuming it changes the tail node
2. Which of the following is the correct syntax to insert a new node at the beginning of a singly linked list in C#?
easy
A. new Node(data).next = head; head = new Node(data);
B. head = new Node(data); head.next = oldHead;
C. head.next = new Node(data);
D. Node newNode = new Node(data); newNode.next = head; head = newNode;

Solution

  1. Step 1: Create new node and link

    Create a new node and set its next pointer to current head.
  2. Step 2: Update head pointer

    Assign head to the new node to make it the first node.
  3. Final Answer:

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

    New node points to old head, head updated [OK]
Hint: New node next = head; head = new node [OK]
Common Mistakes:
  • Not updating head after creating new node
  • Assigning head before setting newNode.next
  • Creating multiple new nodes unnecessarily
3. What will be the output of the following code snippet?
class Node {
  public int data;
  public Node next;
  public Node(int d) { data = d; next = null; }
}

Node head = new Node(10);
head.next = new Node(20);
Node newNode = new Node(5);
newNode.next = head;
head = newNode;

Node temp = head;
while(temp != null) {
  Console.Write(temp.data + " -> ");
  temp = temp.next;
}
Console.WriteLine("null");
medium
A. 5 -> 10 -> 20 -> null
B. 20 -> 10 -> 5 -> null
C. 10 -> 20 -> 5 -> null
D. 5 -> 20 -> 10 -> null

Solution

  1. Step 1: Initial list creation

    Head points to 10, which points to 20 (10 -> 20 -> null).
  2. Step 2: Insert new node at beginning

    New node with data 5 points to old head (10), then head updated to new node (5).
  3. Step 3: Traverse and print list

    Traversal prints 5 -> 10 -> 20 -> null.
  4. Final Answer:

    5 -> 10 -> 20 -> null -> Option A
  5. Quick Check:

    New head 5, followed by 10 and 20 [OK]
Hint: New node points to old head, becomes new head [OK]
Common Mistakes:
  • Confusing order of nodes after insertion
  • Not updating head pointer
  • Printing nodes before insertion
4. Identify the error in the following code that tries to insert a node at the beginning of a singly linked list:
Node newNode = new Node(15);
newNode.next = newNode;
head = newNode;
medium
A. head should be assigned before setting newNode.next
B. Node constructor is missing data parameter
C. newNode.next should point to head, not newNode
D. No error, code is correct

Solution

  1. Step 1: Analyze newNode.next assignment

    newNode.next points to itself, creating a cycle instead of linking to the list.
  2. Step 2: Correct pointer assignment

    newNode.next should point to current head to insert at beginning properly.
  3. Final Answer:

    newNode.next should point to head, not newNode -> Option C
  4. Quick Check:

    Link new node to old head, not itself [OK]
Hint: newNode.next = head, not newNode [OK]
Common Mistakes:
  • Setting next pointer to newNode itself
  • Not updating head after insertion
  • Confusing order of assignments
5. You have a singly linked list with nodes containing values [30, 40, 50]. You want to insert nodes with values [10, 20] at the beginning in that order so the final list is [10, 20, 30, 40, 50]. Which sequence of insertions achieves this?
hard
A. Insert 20 at beginning, then insert 10 at beginning
B. Insert 10 at beginning, then insert 20 at beginning
C. Insert 10 at end, then insert 20 at beginning
D. Insert 20 at end, then insert 10 at beginning

Solution

  1. Step 1: Understand insertion order effect

    Each insert at beginning places new node before current head.
  2. Step 2: Insert 10 first, then 20

    Inserting 10 first results in list [10, 30, 40, 50]. Then inserting 20 at beginning results in [20, 10, 30, 40, 50].
  3. Step 3: Reverse the order to get desired list

    To get [10, 20, 30, 40, 50], insert 20 first, then 10.
  4. Final Answer:

    Insert 20 at beginning, then insert 10 at beginning -> Option A
  5. Quick Check:

    Last inserted node becomes new head [OK]
Hint: Insert last value first to keep order at head [OK]
Common Mistakes:
  • Inserting in given order causes reversed nodes
  • Confusing insert at beginning with insert at end
  • Not updating head after each insertion
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