Bird
Raised Fist0
DSA C#programming

Insert at End of Circular Linked List in DSA C#

Choose your learning style10 modes available
Mental Model
A circular linked list connects the last node back to the first, forming a loop. Inserting at the end means adding a new node just before the first node, then linking it back to the start.
Analogy: Imagine a round table where people sit in a circle. Adding a new person at the end means placing them just before the first person, so the circle stays unbroken.
head -> 1 -> 2 -> 3 -> (back to head 1)
          ↑                   ↓
          ←-------------------
Dry Run Walkthrough
Input: list: 1 -> 2 -> 3 (circular), insert value 4 at end
Goal: Add a new node with value 4 at the end, keeping the circle intact
Step 1: start at head node 1
head -> [1] -> 2 -> 3 -> (back to head 1)
Why: We begin traversal from the head to find the last node
Step 2: move to node 2
head -> 1 -> [2] -> 3 -> (back to head 1)
Why: We keep moving forward to find the last node before head
Step 3: move to node 3
head -> 1 -> 2 -> [3] -> (back to head 1)
Why: Node 3 points back to head, so it is the last node
Step 4: insert new node 4 after node 3
head -> 1 -> 2 -> 3 -> [4] -> (back to head 1)
Why: New node 4 becomes the last node, linking back to head
Result:
head -> 1 -> 2 -> 3 -> 4 -> (back to head 1)
Annotated Code
DSA C#
using System;

public class Node
{
    public int data;
    public Node next;

    public Node(int data)
    {
        this.data = data;
        this.next = null;
    }
}

public class CircularLinkedList
{
    public Node head = null;

    public void InsertAtEnd(int value)
    {
        Node newNode = new Node(value);

        // If list is empty, new node points to itself and becomes head
        if (head == null)
        {
            newNode.next = newNode;
            head = newNode;
            return;
        }

        // Otherwise, find last node which points back to head
        Node curr = head;
        while (curr.next != head)
        {
            curr = curr.next; // advance curr to next node
        }

        // Insert newNode after last node and link back to head
        curr.next = newNode;
        newNode.next = head;
    }

    public void PrintList()
    {
        if (head == null)
        {
            Console.WriteLine("List is empty");
            return;
        }

        Node curr = head;
        do
        {
            Console.Write(curr.data + " -> ");
            curr = curr.next;
        } while (curr != head);
        Console.WriteLine("(back to head " + head.data + ")");
    }
}

public class Program
{
    public static void Main()
    {
        CircularLinkedList cll = new CircularLinkedList();
        cll.InsertAtEnd(1);
        cll.InsertAtEnd(2);
        cll.InsertAtEnd(3);
        cll.InsertAtEnd(4);
        cll.PrintList();
    }
}
if (head == null) { newNode.next = newNode; head = newNode; return; }
handle empty list by making new node point to itself and become head
while (curr.next != head) { curr = curr.next; // advance curr to next node }
traverse to find the last node which points back to head
curr.next = newNode; newNode.next = head;
insert new node after last node and link it back to head to keep circle
OutputSuccess
1 -> 2 -> 3 -> 4 -> (back to head 1)
Complexity Analysis
Time: O(n) because we traverse all nodes once to find the last node
Space: O(1) because we only create one new node and use constant extra space
vs Alternative: Compared to inserting at head which is O(1), inserting at end requires traversal making it O(n)
Edge Cases
empty list
new node becomes head and points to itself
DSA C#
if (head == null)
        {
            newNode.next = newNode;
            head = newNode;
            return;
        }
single node list
new node inserted after the single node and points back to head
DSA C#
while (curr.next != head)
        {
            curr = curr.next; // advance curr to next node
        }
When to Use This Pattern
When you see a circular linked list and need to add a node at the end, use traversal to find the last node that points back to head, then insert the new node there to keep the circle intact.
Common Mistakes
Mistake: Not updating the new node's next pointer to head, breaking the circle
Fix: Always set newNode.next = head after insertion to maintain circular link
Mistake: Not handling empty list case, causing null reference errors
Fix: Check if head is null and initialize new node to point to itself and become head
Summary
Adds a new node at the end of a circular linked list, maintaining the circular link.
Use when you want to append data to the end of a circular linked list.
The last node always points back to head, so insert new node after last and link it to head.

Practice

(1/5)
1. What happens when you insert a new node at the end of a circular linked list?
easy
A. The new node points back to the head, maintaining the circular structure.
B. The new node points to null, breaking the circular structure.
C. The new node becomes the head and points to null.
D. The new node is inserted at the beginning, not the end.

Solution

  1. Step 1: Understand circular linked list structure

    In a circular linked list, the last node points back to the head node, forming a loop.
  2. Step 2: Insert new node at the end

    The new node is linked after the current last node, and its next pointer is set to the head, preserving the circle.
  3. Final Answer:

    The new node points back to the head, maintaining the circular structure. -> Option A
  4. Quick Check:

    New node next = head keeps list circular [OK]
Hint: New end node must link to head to keep circle [OK]
Common Mistakes:
  • Setting new node's next to null breaks the circle
  • Inserting new node at head instead of end
  • Forgetting to update last node's next pointer
2. Which of the following C# code snippets correctly inserts a new node at the end of a circular linked list?
easy
A. newNode.next = head; lastNode.next = newNode;
B. newNode.next = null; lastNode.next = newNode;
C. newNode.next = lastNode; head.next = newNode;
D. newNode.next = head.next; lastNode.next = head;

Solution

  1. Step 1: Set new node's next to head

    To maintain circularity, newNode.next must point to head node.
  2. Step 2: Link last node to new node

    Update lastNode.next to newNode to insert at end.
  3. Final Answer:

    newNode.next = head; lastNode.next = newNode; -> Option A
  4. Quick Check:

    New node points to head, last node points to new node [OK]
Hint: New node next = head; last node next = new node [OK]
Common Mistakes:
  • Setting new node next to null breaks circle
  • Linking new node to last node instead of head
  • Not updating last node's next pointer
3. Given the circular linked list: 10 -> 20 -> 30 -> back to 10, what is the list after inserting 40 at the end?
medium
A. 40 -> 10 -> 20 -> 30 -> back to 40
B. 10 -> 20 -> 30 -> back to 40
C. 10 -> 20 -> 30 -> 40 -> back to 10
D. 10 -> 20 -> 30 -> 40 -> null

Solution

  1. Step 1: Identify current circular list

    List is 10 -> 20 -> 30, last node 30 points back to 10.
  2. Step 2: Insert 40 at end

    New node 40 is linked after 30, and 40 points back to 10 to keep circle.
  3. Final Answer:

    10 -> 20 -> 30 -> 40 -> back to 10 -> Option C
  4. Quick Check:

    New node added at end, circle maintained [OK]
Hint: New node added after last, points to head [OK]
Common Mistakes:
  • Making new node head instead of last
  • Breaking circle by pointing new node to null
  • Forgetting to update last node's next pointer
4. What is wrong with this code snippet for inserting at the end of a circular linked list?
newNode.next = null;
lastNode.next = newNode;
newNode.next = head;
medium
A. The code correctly inserts the node without issues.
B. Setting newNode.next to null breaks the circular link temporarily.
C. newNode.next should point to lastNode, not head.
D. lastNode.next should not be updated before newNode.next.

Solution

  1. Step 1: Analyze newNode.next assignment

    Setting newNode.next = null breaks the circular link temporarily, which is unnecessary.
  2. Step 2: Check order of pointer updates

    Correct insertion requires newNode.next to point to head immediately to maintain circle.
  3. Final Answer:

    Setting newNode.next to null breaks the circular link temporarily. -> Option B
  4. Quick Check:

    newNode.next must never be null in circular list [OK]
Hint: Never set new node's next to null in circular list [OK]
Common Mistakes:
  • Assigning null to newNode.next
  • Updating lastNode.next before setting newNode.next
  • Confusing pointer directions
5. You have a circular linked list with only one node (value 5) pointing to itself. After inserting a new node with value 10 at the end, what is the correct structure?
hard
A. 5 -> 10 -> null
B. 5 -> back to 10 -> back to 5
C. 10 -> 5 -> back to 10
D. 5 -> 10 -> back to 5

Solution

  1. Step 1: Understand single-node circular list

    Initially, node 5 points to itself, forming a circle.
  2. Step 2: Insert new node 10 at end

    New node 10 is linked after 5, and 10 points back to 5 to maintain circularity.
  3. Final Answer:

    5 -> 10 -> back to 5 -> Option D
  4. Quick Check:

    Two nodes linked circularly, last points to head [OK]
Hint: New node next = head even if list has one node [OK]
Common Mistakes:
  • Pointing new node to null instead of head
  • Not updating original node's next pointer
  • Confusing order of nodes after 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