• Last Updated : 27 Sep, 2023

A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. More on Linked List Question 1

What does the following function do for a given Linked List with first node as head?

```void fun1(struct node* head)
{
return;

}``` Prints all nodes of linked lists Prints all nodes of linked list in reverse order Prints alternate nodes of Linked List Prints alternate nodes in reverse order

Question 1-Explanation:

This function fun1 recursively prints the elements of a linked list in reverse order.

It first checks if the head is NULL, and if it is, it returns without doing anything. Otherwise, it calls the fun1 function recursively with the next node in the linked list (head->next). This continues until the last node is reached, which is the node whose next pointer is NULL.

At this point, the function starts returning, and as it returns it prints the value of each node in the linked list starting from the last node, working its way backwards to the first node. This is achieved through the printf statement which prints the value of head->data.

Thus, when this function is called with the head of a linked list as an argument, it will print the values of the nodes in reverse order.

 Question 2

Which of the following points is/are true about Linked List data structure when it is compared with array? Arrays have better cache locality that can make them better in terms of performance. It is easy to insert and delete elements in Linked List Random access is not allowed in a typical implementation of Linked Lists The size of array has to be pre-decided, linked lists can change their size any time. All of the above

Question 2-Explanation:

The following points are true when comparing Linked List data structure with an array:

Insertion and deletion: Linked lists allow for efficient insertion and deletion operations at any point in the list, as they involve simply adjusting pointers, while in an array, these operations can be expensive as all the elements after the insertion or deletion point need to be shifted.

Memory allocation: Linked lists use dynamic memory allocation, so they can grow or shrink as needed, while arrays have a fixed size and need to be allocated a contiguous block of memory upfront.

Access time: Arrays provide constant-time access to any element in the array (assuming the index is known), while accessing an element in a linked list takes linear time proportional to the number of elements in the list, as the list needs to be traversed from the beginning to find the desired element.

Random access: Arrays support random access, which means that we can directly access any element in the array with its index, while linked lists do not support random access and we need to traverse the list to find a specific element.

 Question 3

Consider the following function that takes reference to head of a Doubly Linked List as parameter. Assume that a node of doubly linked list has previous pointer as prev and next pointer as next

## C

```void fun(struct node **head_ref)
{
struct node *temp = NULL;

while (current !=  NULL)
{
temp = current->prev;
current->prev = current->next;
current->next = temp;
current = current->prev;
}

if(temp != NULL )
}
```

Assume that reference of head of following doubly linked list is passed to above function 1 <--> 2 <--> 3 <--> 4 <--> 5 <-->6. What should be the modified linked list after the function call? 2 <--> 1 <--> 4 <--> 3 <--> 6 <-->5 5 <--> 4 <--> 3 <--> 2 <--> 1 <-->6. 6 <--> 5 <--> 4 <--> 3 <--> 2 <--> 1. 6 <--> 5 <--> 4 <--> 3 <--> 1 <--> 2

Question 3-Explanation:

This function fun takes a double pointer to the head of a doubly linked list as its argument and reverses the order of the nodes in the list.

For more reference:

 Question 4

Which of the following sorting algorithms can be used to sort a random linked list with minimum time complexity? Insertion Sort Quick Sort Heap Sort Merge Sort

Question 4-Explanation:

Both Merge sort and Insertion sort can be used for linked lists. The slow random-access performance of a linked list makes other algorithms (such as quicksort) perform poorly, and others (such as heapsort) completely impossible. Since worst case time complexity of Merge Sort is O(nLogn) and Insertion sort is O(n^2), merge sort is preferred. See following for implementation of merge sort using Linked List.

 Question 5

The following function reverse() is supposed to reverse a singly linked list. There is one line missing at the end of the function.

## C

```/* Link list node */
struct node
{
int data;
struct node* next;
};

/* head_ref is a double pointer which points to head (or start) pointer
{
struct node* prev   = NULL;
struct node* next;
while (current != NULL)
{
next  = current->next;
current->next = prev;
prev = current;
current = next;
}
}

```

What should be added in place of "/*ADD A STATEMENT HERE*/", so that the function correctly reverses a linked list. *head_ref = prev; *head_ref = current; *head_ref = next; *head_ref = NULL;

Question 5-Explanation:

The statement to update the head pointer could be as follows

This statement sets the value of *head_ref (which is a double pointer) to the value of prev, which is the new head of the reversed list.

 Question 6

What is the output of following function in which start is pointing to the first node of the following linked list 1->2->3->4->5->6 ?

## C

```void fun(struct node* start)
{
if(start == NULL)
return;
printf("%d  ", start->data);

if(start->next != NULL )
fun(start->next->next);
printf("%d  ", start->data);
}
``` 1 4 6 6 4 1 1 3 5 1 3 5 1 2 3 5 1 3 5 5 3 1

Question 6-Explanation:

The function prints the data of the current node and then recursively calls itself with the second next node (i.e., start->next->next).

So, it prints the data of every alternate node of the linked list i.e 1 3 5, and then, since the next->next of 5 is null, it returns and prints the data of the current node, so it then prints 5 3 1.

Therefore, for the given linked list 1->2->3->4->5->6, the function would print 1 3 5 5 3 1.

 Question 7

The following C function takes a simply-linked list as input argument. It modifies the list by moving the last element to the front of the list and returns the modified list. Some part of the code is left blank. Choose the correct alternative to replace the blank line.

## C

```typedef struct node
{
int value;
struct node *next;
}Node;

{
Node *p, *q;
q = NULL; p = head;
while (p-> next !=NULL)
{
q = p;
p = p->next;
}
_______________________________
}
``` q = NULL; p->next = head; head = p; q->next = NULL; head = p; p->next = head; head = p; p->next = q; q->next = NULL; q->next = NULL; p->next = head; head = p;

Question 7-Explanation:

To move the last element to the front of the list, we need to do the following steps:

1. Make the second last node as the last node (i.e., set its next pointer to NULL).
2. Set the next pointer of the current last node (which is now the second last node) to the original head node.
3. Make the current last node as the new head node.
 Question 8

The following  function takes a single-linked list of integers as a parameter and rearranges the elements of the list. The function is called with the list containing the integers 1, 2, 3, 4, 5, 6, 7 in the given order. What will be the contents of the list after the function completes execution?

## Java

```class Node {
int value;
Node next;
}

void rearrange(Node list) {
Node p, q;
int temp;
if (list == null || list.next == null) {
return;
}
p = list;
q = list.next;
while (q != null) {
temp = p.value;
p.value = q.value;
q.value = temp;
p = q.next;
q = p != null ? p.next : null;
}
}
``` 1,2,3,4,5,6,7 2,1,4,3,6,5,7 1,3,2,5,4,7,6 2,3,4,5,6,7,1

Question 8-Explanation:

The function rearrange() exchanges data of every node with its next node. It starts exchanging data from the first node itself.
For eg. 3,5,7,9,11

 Question 9

In the worst case, the number of comparisons needed to search a singly linked list of length n for a given element is (GATE CS 2002) log(2*n) n/2 log(2*n) -1 n

Question 9-Explanation:

In the worst case, the given element may not be present in the singly linked list of length n. Therefore, the search algorithm would need to traverse the entire list to determine that the element is not present.

Therefore, the number of comparisons needed to search a singly linked list of length n in the worst case is equal to n, which is the length of the list.

 Question 10

Suppose each set is represented as a linked list with elements in arbitrary order. Which of the operations among union, intersection, membership, cardinality will be the slowest? (GATE CS 2004) union membership cardinality union, intersection

Question 10-Explanation:

Cardinality and membership are definitely not the slowest one. For cardinality, just count the number of nodes in a list. For membership, just traverse the list and look for a match
For getting intersection of L1 and L2, search for each element of L1 in L2 and print the elements we find in L2.
There can be many ways for getting union of L1 and L2. One of them is as follows
a) Print all the nodes of L1 and print only those which are not present in L2.
b) Print nodes of L2.

There are 42 questions to complete.