Bird
Raised Fist0
DSA C++programming~20 mins

Insert at End of Circular Linked List in DSA C++ - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Circular Linked List 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 end of a circular linked list
What is the printed state of the circular linked list after inserting 10, 20, and 30 at the end?
DSA C++
struct Node {
    int data;
    Node* next;
    Node(int val) : data(val), next(nullptr) {}
};

class CircularLinkedList {
public:
    Node* tail;
    CircularLinkedList() : tail(nullptr) {}

    void insertEnd(int val) {
        Node* newNode = new Node(val);
        if (!tail) {
            tail = newNode;
            tail->next = tail;
        } else {
            newNode->next = tail->next;
            tail->next = newNode;
            tail = newNode;
        }
    }

    void printList() {
        if (!tail) return;
        Node* temp = tail->next;
        do {
            std::cout << temp->data << " -> ";
            temp = temp->next;
        } while (temp != tail->next);
        std::cout << "null" << std::endl;
    }
};

int main() {
    CircularLinkedList cll;
    cll.insertEnd(10);
    cll.insertEnd(20);
    cll.insertEnd(30);
    cll.printList();
    return 0;
}
A20 -> 30 -> 10 -> null
B30 -> 20 -> 10 -> null
C10 -> 20 -> null
D10 -> 20 -> 30 -> null
Attempts:
2 left
💡 Hint
Remember that in a circular linked list, the tail's next points to the head, so printing starts from tail->next.
❓ Predict Output
intermediate
2:00remaining
Output after inserting a single node in an empty circular linked list
What is the printed state of the circular linked list after inserting only one node with value 5 at the end?
DSA C++
struct Node {
    int data;
    Node* next;
    Node(int val) : data(val), next(nullptr) {}
};

class CircularLinkedList {
public:
    Node* tail;
    CircularLinkedList() : tail(nullptr) {}

    void insertEnd(int val) {
        Node* newNode = new Node(val);
        if (!tail) {
            tail = newNode;
            tail->next = tail;
        } else {
            newNode->next = tail->next;
            tail->next = newNode;
            tail = newNode;
        }
    }

    void printList() {
        if (!tail) return;
        Node* temp = tail->next;
        do {
            std::cout << temp->data << " -> ";
            temp = temp->next;
        } while (temp != tail->next);
        std::cout << "null" << std::endl;
    }
};

int main() {
    CircularLinkedList cll;
    cll.insertEnd(5);
    cll.printList();
    return 0;
}
A5 -> null
BError: infinite loop
C5 -> 5 -> null
Dnull
Attempts:
2 left
💡 Hint
A single node in a circular linked list points to itself.
🔧 Debug
advanced
2:00remaining
Identify the bug causing infinite loop in circular linked list insertion
Given the following insertEnd function, what is the bug causing an infinite loop when printing the list?
DSA C++
void insertEnd(int val) {
    Node* newNode = new Node(val);
    if (!tail) {
        tail = newNode;
        tail->next = nullptr;
    } else {
        newNode->next = tail->next;
        tail->next = newNode;
        tail = newNode;
    }
}
AnewNode->next is not assigned, causing undefined behavior
Btail->next is set to nullptr instead of pointing to the new node, breaking circularity
Ctail is not updated to newNode, so list order is wrong
DThe function does not handle empty list case
Attempts:
2 left
💡 Hint
In a circular linked list, the last node's next must point to the head node, not nullptr.
🧠 Conceptual
advanced
2:00remaining
Understanding tail pointer role in circular linked list insertion
Why is the tail pointer updated to the new node after inserting at the end of a circular linked list?
ABecause updating tail to new node breaks the circular link intentionally
BBecause tail always points to the head node in a circular linked list
CBecause the new node becomes the last node, so tail must point to it to maintain correct insertion point
DBecause tail points to the node before the head node
Attempts:
2 left
💡 Hint
Think about where new nodes are added and how tail helps in insertion.
❓ Predict Output
expert
2:00remaining
Output after multiple insertions and one traversal in circular linked list
What is the output of the following code snippet?
DSA C++
struct Node {
    int data;
    Node* next;
    Node(int val) : data(val), next(nullptr) {}
};

class CircularLinkedList {
public:
    Node* tail;
    CircularLinkedList() : tail(nullptr) {}

    void insertEnd(int val) {
        Node* newNode = new Node(val);
        if (!tail) {
            tail = newNode;
            tail->next = tail;
        } else {
            newNode->next = tail->next;
            tail->next = newNode;
            tail = newNode;
        }
    }

    int countNodes() {
        if (!tail) return 0;
        int count = 0;
        Node* temp = tail->next;
        do {
            count++;
            temp = temp->next;
        } while (temp != tail->next);
        return count;
    }
};

int main() {
    CircularLinkedList cll;
    cll.insertEnd(1);
    cll.insertEnd(2);
    cll.insertEnd(3);
    cll.insertEnd(4);
    int total = cll.countNodes();
    std::cout << total << std::endl;
    return 0;
}
A4
B3
C5
D0
Attempts:
2 left
💡 Hint
Count nodes by traversing from tail->next until you reach the start again.

Practice

(1/5)
1. What happens when you insert a new node at the end of an empty circular linked list?
easy
A. The new node points to itself, becoming the only node in the list.
B. The new node points to null, ending the list.
C. The new node is discarded because the list is empty.
D. The new node points to the head, but the list remains empty.

Solution

  1. Step 1: Understand circular linked list empty state

    When the list is empty, there is no node, so the new node must point to itself to form a circle.
  2. Step 2: Insert new node and update pointers

    The new node's next pointer is set to itself, making it the only node and the head of the list.
  3. Final Answer:

    The new node points to itself, becoming the only node in the list. -> Option A
  4. Quick Check:

    Empty list insertion creates a self-pointing node [OK]
Hint: New node points to itself if list is empty [OK]
Common Mistakes:
  • Setting next pointer to null instead of itself
  • Not updating the head pointer
  • Assuming list remains empty after insertion
2. Which of the following is the correct syntax to insert a new node at the end of a circular linked list in C++?
easy
A. newNode->next = head; tail->next = newNode; tail = newNode;
B. newNode->next = nullptr; tail->next = newNode; tail = newNode;
C. newNode->next = tail; head->next = newNode; tail = newNode;
D. newNode->next = head; head->next = newNode; tail = newNode;

Solution

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

    In a circular linked list, the last node points to the head, so newNode->next = head.
  2. Step 2: Update current tail's next and tail pointer

    Set tail->next to newNode to link the last node to new node, then update tail to newNode.
  3. Final Answer:

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

    New node points to head, tail updated [OK]
Hint: Tail's next must point to new node, new node points to head [OK]
Common Mistakes:
  • Setting newNode->next to nullptr instead of head
  • Updating head->next instead of tail->next
  • Not updating tail pointer after insertion
3. Given the circular linked list with nodes 10 -> 20 -> 30 -> (back to 10), what will be 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 -> null
D. 10 -> 20 -> 30 -> 40 -> (back to 10)

Solution

  1. Step 1: Identify current circular list structure

    The list is 10 -> 20 -> 30, and 30 points back to 10 forming a circle.
  2. Step 2: Insert 40 at end and update pointers

    New node 40 points to head (10), old tail (30) points to 40, making list 10 -> 20 -> 30 -> 40 -> back to 10.
  3. Final Answer:

    10 -> 20 -> 30 -> 40 -> (back to 10) -> Option D
  4. Quick Check:

    New node added at end, circle maintained [OK]
Hint: New node points to head, old tail points to new node [OK]
Common Mistakes:
  • Breaking the circle by pointing last node to null
  • Inserting new node at start instead of end
  • Not updating tail pointer
4. What is wrong with this code snippet for inserting at the end of a circular linked list?
void insertEnd(Node*& tail, int data) {
  Node* newNode = new Node(data);
  newNode->next = tail->next;
  tail = newNode;
}
medium
A. It sets newNode->next to null instead of head.
B. It deletes the old tail node accidentally.
C. It does not update the old tail's next pointer to newNode.
D. It inserts the new node at the beginning instead of the end.

Solution

  1. Step 1: Check pointer updates in code

    The code sets newNode->next to tail->next (head), but does not update tail->next to newNode.
  2. Step 2: Identify missing link update

    Without updating old tail's next pointer, the list breaks the circle and newNode is not linked properly.
  3. Final Answer:

    It does not update the old tail's next pointer to newNode. -> Option C
  4. Quick Check:

    Old tail's next must point to new node [OK]
Hint: Always update old tail's next to new node [OK]
Common Mistakes:
  • Forgetting to update old tail's next pointer
  • Setting newNode->next incorrectly
  • Not updating tail pointer after insertion
5. You have a circular linked list with nodes 5 -> 15 -> 25 -> (back to 5). You want to insert a new node with value 35 at the end. After insertion, you mistakenly forget to update the tail pointer to the new node. What will be the effect on the list?
hard
A. The list will have 5 -> 15 -> 25 -> (back to 5) without 35.
B. The list will have 5 -> 15 -> 25 -> 35 -> (back to 5), but tail still points to 25.
C. The new node 35 will not be linked and will be lost.
D. The list will break and become linear ending at 25.

Solution

  1. Step 1: Insert new node and link old tail

    New node 35 is linked after 25, and points back to head (5), so list is 5 -> 15 -> 25 -> 35 -> back to 5.
  2. Step 2: Tail pointer not updated

    Tail still points to 25, so operations relying on tail may behave incorrectly, but list structure is intact.
  3. Final Answer:

    The list will have 5 -> 15 -> 25 -> 35 -> (back to 5), but tail still points to 25. -> Option B
  4. Quick Check:

    Tail pointer must update, else points to old last node [OK]
Hint: Tail pointer must update to new node after insertion [OK]
Common Mistakes:
  • Assuming list breaks without tail update
  • Thinking new node is lost if tail not updated
  • Confusing tail pointer with node links
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