Bird
Raised Fist0
DSA C#programming~20 mins

Insert at End Tail Insert in DSA C# - Practice Problems & Challenges

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

class LinkedList {
    public Node head;

    public void InsertAtEnd(int data) {
        Node newNode = new Node(data);
        if (head == null) {
            head = newNode;
            return;
        }
        Node temp = head;
        while (temp.next != null) {
            temp = temp.next;
        }
        temp.next = newNode;
    }

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

var list = new LinkedList();
list.InsertAtEnd(4);
list.InsertAtEnd(5);
list.InsertAtEnd(6);
list.PrintList();
A4 -> 5 -> 6 -> null
B6 -> 5 -> 4 -> null
C4 -> 5 -> null
Dnull
Attempts:
2 left
💡 Hint
Think about how nodes are added at the end and how the list is printed from head to tail.
❓ Predict Output
intermediate
2:00remaining
Resulting List After Multiple Tail Inserts
What will be the output after inserting 10, 20, and 30 at the end of an initially empty linked list?
DSA C#
class Node {
    public int data;
    public Node next;
    public Node(int d) { data = d; next = null; }
}

class LinkedList {
    public Node head;

    public void InsertAtEnd(int data) {
        Node newNode = new Node(data);
        if (head == null) {
            head = newNode;
            return;
        }
        Node current = head;
        while (current.next != null) {
            current = current.next;
        }
        current.next = newNode;
    }

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

var list = new LinkedList();
list.InsertAtEnd(10);
list.InsertAtEnd(20);
list.InsertAtEnd(30);
list.PrintList();
A10 -> 20 -> null
B30 -> 20 -> 10 -> null
Cnull
D10 -> 20 -> 30 -> null
Attempts:
2 left
💡 Hint
Remember that each new node is added after the last node.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Tail Insert Method
What error will occur when running this code that inserts nodes at the end of a linked list?
DSA C#
class Node {
    public int data;
    public Node next;
    public Node(int d) { data = d; next = null; }
}

class LinkedList {
    public Node head;

    public void InsertAtEnd(int data) {
        Node newNode = new Node(data);
        if (head == null) {
            head = newNode;
            return;
        }
        Node temp = head;
        while (temp.next == null) {
            temp = temp.next;
        }
        temp.next = newNode;
    }
}

var list = new LinkedList();
list.InsertAtEnd(1);
list.InsertAtEnd(2);
AInfinite loop
BNo error, list inserts correctly
CSyntaxError
DNullReferenceException
Attempts:
2 left
💡 Hint
Check the while loop condition carefully.
📝 Syntax
advanced
2:00remaining
Syntax Error in Tail Insert Implementation
Which option contains the correct syntax to insert a node at the end of a linked list?
DSA C#
class Node {
    public int data;
    public Node next;
    public Node(int d) { data = d; next = null; }
}

class LinkedList {
    public Node head;

    public void InsertAtEnd(int data) {
        Node newNode = new Node(data);
        if (head == null) {
            head = newNode;
            return;
        }
        Node temp = head;
        while (temp.next != null) {
            temp = temp.next;
        }
        temp.next = newNode;
    }
}
Awhile temp.next != null: temp = temp.next
Bwhile (temp.next != null) { temp = temp.next; }
Cwhile (temp.next != null) temp = temp.next;
Dwhile temp.next != null { temp = temp.next }
Attempts:
2 left
💡 Hint
Remember C# syntax for while loops requires parentheses and braces.
🚀 Application
expert
3:00remaining
Tail Insert in Circular Linked List
After inserting 3 nodes with values 7, 8, and 9 at the end of an initially empty circular linked list, what is the printed output when traversing starting from head and stopping after 3 nodes?
DSA C#
class Node {
    public int data;
    public Node next;
    public Node(int d) { data = d; next = null; }
}

class CircularLinkedList {
    public Node head;

    public void InsertAtEnd(int data) {
        Node newNode = new Node(data);
        if (head == null) {
            head = newNode;
            newNode.next = head;
            return;
        }
        Node temp = head;
        while (temp.next != head) {
            temp = temp.next;
        }
        temp.next = newNode;
        newNode.next = head;
    }

    public void PrintList(int count) {
        Node temp = head;
        for (int i = 0; i < count; i++) {
            System.Console.Write(temp.data + " -> ");
            temp = temp.next;
        }
        System.Console.WriteLine("(back to head)");
    }
}

var clist = new CircularLinkedList();
clist.InsertAtEnd(7);
clist.InsertAtEnd(8);
clist.InsertAtEnd(9);
clist.PrintList(3);
A9 -> 8 -> 7 -> (back to head)
B7 -> 8 -> (back to head)
C7 -> 8 -> 9 -> (back to head)
Dnull
Attempts:
2 left
💡 Hint
In a circular linked list, the last node points back to head, so traversal cycles through nodes.

Practice

(1/5)
1. What does the InsertAtEnd method do in a singly linked list?
easy
A. Adds a new node at the end (tail) of the list
B. Removes the first node of the list
C. Inserts a new node at the beginning (head) of the list
D. Deletes the last node of the list

Solution

  1. Step 1: Understand the method name

    The method name InsertAtEnd suggests adding a node at the end of the list.
  2. Step 2: Recall linked list operations

    Inserting at the end means adding a new node after the current last node (tail).
  3. Final Answer:

    Adds a new node at the end (tail) of the list -> Option A
  4. Quick Check:

    InsertAtEnd = Add node at tail [OK]
Hint: InsertAtEnd means add node at tail, not head or remove [OK]
Common Mistakes:
  • Confusing insert at end with insert at beginning
  • Thinking it removes nodes instead of adding
  • Mixing up tail and head operations
2. Which of the following is the correct syntax to create a new node and insert it at the end of a singly linked list in C#?
easy
A. Node newNode = new Node(data); tail.next = newNode; tail = newNode;
B. Node newNode = new Node(data); newNode.next = head;
C. Node newNode = new Node(data); newNode.prev = tail; tail = newNode;
D. Node newNode = new Node(data); head = newNode;

Solution

  1. Step 1: Create a new node with data

    We create a new node using new Node(data).
  2. Step 2: Link new node at the end

    To insert at end, set tail.next = newNode and update tail = newNode.
  3. Final Answer:

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

    Insert at tail = tail.next = newNode; tail = newNode [OK]
Hint: Tail insert needs tail.next and tail update [OK]
Common Mistakes:
  • Assigning newNode.next to head instead of tail.next
  • Forgetting to update tail pointer
  • Using prev pointer in singly linked list
3. What will be the output linked list after executing the following code?
LinkedList list = new LinkedList();
list.InsertAtEnd(10);
list.InsertAtEnd(20);
list.InsertAtEnd(30);
list.PrintList();

Assuming PrintList() prints nodes as value -> value -> ... -> null.
medium
A. 30 -> 20 -> 10 -> null
B. 10 -> 20 -> 30 -> null
C. 10 -> 20 -> null
D. null

Solution

  1. Step 1: Insert nodes at end in order

    Nodes 10, 20, 30 are inserted at the tail one by one, so list order is 10, then 20, then 30.
  2. Step 2: Print list in order

    Printing shows nodes in insertion order ending with null.
  3. Final Answer:

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

    InsertAtEnd preserves order: 10,20,30 [OK]
Hint: InsertAtEnd keeps order; print shows same sequence [OK]
Common Mistakes:
  • Assuming insert at end reverses order
  • Missing last node in print
  • Confusing head and tail positions
4. The following code tries to insert a node at the end of a singly linked list but causes a runtime error. What is the error?
public void InsertAtEnd(int data) {
    Node newNode = new Node(data);
    if (head == null) {
        head = newNode;
    } else {
        Node temp = head;
        while (temp.next != null) {
            temp = temp.next;
        }
        temp = newNode;
    }
}
medium
A. Node constructor is missing
B. The head is not updated when list is empty
C. The while loop condition is incorrect
D. The line temp = newNode; does not link the new node to the list

Solution

  1. Step 1: Analyze the while loop

    The loop finds the last node where temp.next == null.
  2. Step 2: Check how new node is linked

    Assigning temp = newNode; only changes local variable, not the last node's next pointer.
  3. Step 3: Correct linking

    Should assign temp.next = newNode; to link new node at end.
  4. Final Answer:

    The line temp = newNode; does not link the new node to the list -> Option D
  5. Quick Check:

    Must set temp.next = newNode, not temp = newNode [OK]
Hint: Assign newNode to temp.next, not temp variable [OK]
Common Mistakes:
  • Assigning newNode to temp instead of temp.next
  • Not updating head when list empty
  • Incorrect loop condition causing infinite loop
5. You have a singly linked list with nodes: 5 -> 15 -> 25 -> null. You want to insert a new node with value 35 at the end. After insertion, what will be the correct sequence of nodes? Also, what changes must be made if the list is empty initially?
hard
A. Sequence: 5 -> 15 -> 25 -> null; No changes needed for empty list
B. Sequence: 35 -> 5 -> 15 -> 25 -> null; If empty, set tail = newNode only
C. Sequence: 5 -> 15 -> 25 -> 35 -> null; If empty, set head = newNode and tail = newNode
D. Sequence: 5 -> 15 -> 25 -> 35 -> null; If empty, set tail.next = newNode only

Solution

  1. Step 1: Insert new node at end

    Adding 35 at end appends it after 25, so list becomes 5 -> 15 -> 25 -> 35 -> null.
  2. Step 2: Handle empty list case

    If list is empty, both head and tail must point to the new node to start the list properly.
  3. Final Answer:

    Sequence: 5 -> 15 -> 25 -> 35 -> null; If empty, set head = newNode and tail = newNode -> Option C
  4. Quick Check:

    Insert at end appends; empty list needs head and tail set [OK]
Hint: Empty list: set head and tail to new node; else tail.next = newNode [OK]
Common Mistakes:
  • Not updating both head and tail when list is empty
  • Inserting new node at head instead of tail
  • Only updating tail without linking new node
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