Quiz On Circular Linked List

Last Updated : 26 Dec, 2023

The circular linked list is a linked list where all nodes are connected to form a circle. In a circular linked list, the first node and the last node are connected to each other which forms a circle. There is no NULL at the end.

More on Circular Linked List

Circular Linked List Quiz

Circular Linked List Quiz

Question 1

Find the functionality of the given code-

C++

void function(int data)
{
    int flag = 0;
    if (head != null) {
        Node temp = head.getNext();
        while ((temp != head)
               && (!(temp.getItem() == data))) {
            temp = temp.getNext();
            flag = 1;
            break;
        }
    }
    if (flag)
        System.out.println("success");
    else
        System.out.println("fail");
}
Cross

Print success if a particular element is not found

Tick

Print fail if a particular element is not found

Cross

Print success if a particular element is equal to 1

Cross

Print fail if the list is empty



Question 1-Explanation: 

The function prints fail if the given element is not found. In the code, we also cover the Edge case, the list is empty.

Hence (B) is the correct answer.

Question 2

What is the time complexity of searching for an element in a circular linked list?

Tick

O(n)

Cross

O(nlogn)

Cross

O(1)

Cross

O(n2)



Question 2-Explanation: 

In the worst case, you have to traverse through the entire list of n elements.

Question 3

Choose the code snippet which inserts a node to the head of the list?

Tick

public void insertHead(int data)
{
Node temp = new Node(data);
Node cur = head;
while(cur.getNext() != head)
 cur = cur.getNext()
if(head == null)
{
 head = temp;
 head.setNext(head);
}
else
{
 temp.setNext(head);
 head = temp;
 cur.setNext(temp);
}
size++;
}
 

Cross

public void insertHead(int data)
{
Node temp = new Node(data);
while(cur != head)
 cur = cur.getNext()
if(head == null)
{
 head = temp;
 head.setNext(head);
}
else
{
 temp.setNext(head.getNext());
 cur.setNext(temp);
}
size++;
}

Cross

public void insertHead(int data)
{
Node temp = new Node(data);
if(head == null)
{
 head = temp;
 head.setNext(head);
}
else
{
 temp.setNext(head.getNext());
 head = temp;
}
size++;
}

Cross

public void insertHead(int data)
{
Node temp = new Node(data);
if(head == null)
{
 head = temp;
 head.setNext(head.getNext());
}
else
{
 temp.setNext(head.getNext());
 head = temp;
}
size++;
}



Question 3-Explanation: 

If the list is empty make the new node as ‘head’, otherwise traverse the list to the end and make its ‘next’ pointer point to the new node, set the new node’s next point to the current head and make the new node as the head.

Question 4

What is the functionality of the following code? Choose the most appropriate answer.

C++

int function()
{
    if (head == null)
        return Integer.MIN_VALUE;
    int var;
    Node temp = head;
    while (temp.getNext() != head)
        temp = temp.getNext();
    if (temp == head) {
        var = head.getItem();
        head = null;
        return var;
    }
    temp.setNext(head.getNext());
    var = head.getItem();
    head = head.getNext();
    return var;
}
Cross

Return data from the end of the list

Cross

Returns the data and deletes the node at the end of the list

Cross

Returns the data from the beginning of the list

Tick

Returns the data and deletes the node from the beginning of the list



Question 4-Explanation: 

First traverse through the list to find the end node, then manipulate the ‘next’ pointer such that it points to the current head’s next node, returns the data stored in the head and make this next node as the head.

Hence Option(D) is the correct answer.

Question 5

Consider a small circular linked list. How to detect the presence of cycles in this list effectively?

Cross

Keep one node as head and traverse another temp node till the end to check if its ‘next points to head

Tick

Have fast and slow pointers with the fast pointer advancing two nodes at a time and slow pointer advancing by one node at a time

Cross

Cannot determine, you have to pre-define if the list contains cycles

Cross

Circular linked list itself represents a cycle. So no new cycles cannot be generated



Question 5-Explanation: 

Advance the pointers in such a way that the fast pointer advances two nodes at a time and slow pointer advances one node at a time and check to see if at any given instant of time if the fast pointer points to slow pointer or if the fast pointer’s ‘next’ points to the slow pointer. This is applicable for smaller lists.

Below is the implementation of the above approach:

 bool isCircular() {
        if (head == nullptr) {
            return false;
        }
        Node* slow = head;
        Node* fast = head->next;
        while (fast != nullptr && fast->next != nullptr) {
            if (slow == fast) {
                return true;
            }
            slow = slow->next;
            fast = fast->next->next;
        }
        return false;
    }

Hence (B) is the correct answer.

Question 6

Which of the following application makes use of a circular linked list?

Cross

Undo Operation

Cross

Recursive function calls

Tick

Allocating CPU to resources

Cross

Implement Hash Tables



Question 6-Explanation: 

Applications of circular linked lists:

  • Multiplayer games use this to give each player a chance to play.
  • A circular linked list can be used to organize multiple running applications on an operating system. These applications are iterated over by the OS.
  • Circular linked lists can be used in resource allocation problems.
  • Circular linked lists are commonly used to implement circular buffers,
  • Circular linked lists can be used in simulation and gaming.

Hence Option (C) is the correct answer.
 

Question 7

In a circular linked list, How many nodes are containing NULL as the address?

Cross

Only one node

Cross

Only Two node

Cross

Every Node

Tick

No, any node



Question 7-Explanation: 

In a circular linked list, there is no any node containing NULL as their address.

Circular linked list

Following are some properties of the circular linked list: 

  • A node always points to another node, so NULL assignment is not necessary.
  • Any node can be set as the starting point.
  • Nodes are traversed quickly from the first to the last.

Hence Option(D) is the correct answer.

Question 8

What is the time complexity to insert a node at the beginning of the list?

Cross

O(LogN)

Cross

O(N)

Tick

O(1)

Cross

None



Question 8-Explanation: 

To insert a node at the beginning of the Circular linked list, there is no need to traverse the list so it takes constant time.

To insert a node at the beginning of the list, follow these steps: 

  • Create a node, say T. 
  • Make T -> next = last -> next. 
  • last -> next = T. 

Circular linked list

And then, 

Circular linked list

Hence Option(A) is the correct answer.

Question 9

Consider the below program, and identify which Operation is performed.

C++

int solve(struct Node* head)
{
    if (head == NULL)
        return 1;
    struct Node* ptr;
    ptr = head->next;
    while (ptr != NULL && ptr != head)
        ptr = ptr->next;
    return (ptr == head);
}
Cross

Checking for List is empty of not

Tick

Checking for the list is Circular or not

Cross

count the number of elements in the list

Cross

None



Question 9-Explanation: 

In the above program, we are just checking whether the linked list is circular or not. The idea is to store the head of the linked list and traverse it. If the iterator reaches NULL, the linked list is not circular. else If it reaches the head again, then the linked list is circular. 

A linked list is called circular if it is not NULL-terminated and all nodes are connected in the form of a cycle. Below is an example of a circular linked list.

 

Hence (B) is the correct answer.

Question 10

Which of the following Statement is correct about Circular Linked List?

P1: Every Node has a successor.
P2: TIme complexity for deleting the first node is O(N).
P3: Last Node points to the NULL.
P4: Every Node contains the data and a next pointer to traverse the list.

Cross

Only P4

Cross

Only P1

Cross

P1 and P2

Tick

P1 and P4.



Question 10-Explanation: 

In the above statement, P1 and P4 are correct about the linked list.

P1:  Every Node has a successor node in a circular form.
P2: TIme complexity for deleting the first node is O(N). this statement is incorrect because deleting the first node will take O(1) time.
P3:Last Node points to the NULL. this statement is also incorrect because the last node points to the first node in a circular linked list.
P4: Every Node contains the data and a next pointer to traverse the list., this statement is also correct.

Therefore, P1 and P4 are the correct statements.

Hence (D) is the correct answer.
 

There are 10 questions to complete.


Share your thoughts in the comments

Similar Reads