# Circular Linked List | Data Structure MCQs

1.

A.) You cannot have the ‘next’ pointer point to null in a circular linked list
B.) It is faster to traverse the circular linked list
C.) You may or may not have the ‘next’ pointer point to null in a circular linked list
D.) All of these

You may or may not have the ‘next’ pointer point to null in a circular linked list
The ‘next’ pointer points to null only when the list is empty, otherwise it points to the head of the list

2.

What is the functionality of the following piece of code? Select the most appropriate

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

C.) Print success if a particular element is equal to 1
D.) Print fail if the list is empty

The function prints fail if the given element is not found. Note that this option is inclusive of option d, the list being empty is one of the cases covered.

3.

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

A.) Undo operation in a text editor
B.) Recursive function calls
C.) Allocating CPU to resources
D.) All of these

Generally, round robin fashion is employed to allocate CPU time to resources which makes use of the circular linked list data structure.

4.

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

```public int function()
{
return Integer.MIN_VALUE;
int var;
temp = temp.getNext();
{
return var;
}
return var;
}```

A.) Return data from the end of the list
B.) Returns the data and deletes the node at the end of the list
C.) Returns the data from the beginning of the list
D.) Returns the data and deletes the node from the beginning of the list

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, return the data stored in head and make this next node as the head.

5.

A.) Every node has a successor
B.) Time complexity of inserting a new node at the head of the list is O(1)
C.) Time complexity for deleting the last node is O(n)
D.) None of these

Time complexity of inserting a new node at the head of the list is O(n) because you have to traverse through the list to find the tail node.

6.

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

A.) Keep one node as head and traverse another temp node till the end to check if its ‘next points to head
B.) 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
C.) Cannot determine, you have to pre-define if the list contains cycles
D.) None of these

Advance the pointers as mentioned in ‘b’, 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. Note that this trick is useful only if the list is small.

7.

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

```public int function()
{
return Integer.MIN_VALUE;
int var;
Node cur;
{
cur = temp;
temp = temp.getNext();
}
{
return var;
}
var = temp.getItem();
return var;
}```

A.) Return data from the end of the list
B.) Returns the data and deletes the node at the end of the list
C.) Returns the data from the beginning of the list
D.) Returns the data and deletes the node from the beginning of the list

First traverse through the list to find the end node, also have a trailing pointer to find the penultimate node,
make this trailing pointer’s ‘next’ point to the head and return the data stored in the ‘temp’ node.

8.

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

A.)
```public void insertHead(int data)
{
Node temp = new Node(data);
cur = cur.getNext()
{
}
else
{
cur.setNext(temp);
}
size++;
}```
B.)
```public void insertHead(int data)
{
Node temp = new Node(data);
cur = cur.getNext()
{
}
else
{
cur.setNext(temp);
}
size++;
}```
C.)
```public void insertHead(int data)
{
Node temp = new Node(data);
{
}
else
{
}
size++;
}```
D.)
```public void insertHead(int data)
{
Node temp = new Node(data);
{
}
else
{