# Data Structure Questions and Answers – Singly Linked List Operations – 2, 3

Data Structure Questions and Answers – Singly Linked List Operations – 2, 3. This set of Data Structure Interview Questions and Answers for freshers focuses on “Singly Linked Lists Operations – 2”.

1. What kind of linked list is best to answer questions like “What is the item at position n?”
d) Array implementation of linked list

Explanation: Arrays provide random access to elements by providing the index value within square brackets. In the linked list, we need to traverse through each element until we reach the nth position. Time taken to access an element represented in arrays is less than the singly, doubly and circular linked lists. Thus, array implementation is used to access the item at position n.

2. Linked lists are not suitable for the implementation of ___________
a) Insertion sort
c) Polynomial manipulation
d) Binary search

Explanation: It cannot be implemented using linked lists.

3. Linked list is considered as an example of ___________ type of memory allocation.
a) Dynamic
b) Static
c) Compile time
d) Heap

Explanation: As memory is allocated at the run time.

4. In Linked List implementation, a node carries information regarding ___________
a) Data
d) Node

Enroll Now

Explanation: A linked list is a collection of objects linked together by references from an object to another object. By convention, these objects are named as nodes. A linked list consists of nodes where each node contains one or more data fields and a reference(link) to the next node.

5. Linked list data structure offers considerable saving in _____________
a) Computational Time
b) Space Utilization
c) Space Utilization and Computational Time
d) Speed Utilization

Explanation: Linked lists saves both space and time.

6. Which of the following points is/are not true about Linked List data structure when it is compared with an array?
a) Arrays have better cache locality that can make them better in terms of performance
b) It is easy to insert and delete elements in Linked List
c) Random access is not allowed in a typical implementation of Linked Lists
d) Access of elements in the linked list takes less time than compared to arrays

Explanation: To access an element in a linked list, we need to traverse every element until we reach the desired element. This will take more time than arrays as arrays provide random access to their elements.

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

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

a) Prints all nodes of linked lists
b) Prints all nodes of linked list in reverse order
c) Prints alternate nodes of Linked List
d) Prints alternate nodes in reverse order

Explanation: fun1() prints the given Linked List in a reverse manner.
For Linked List 1->2->3->4->5, fun1() prints 5->4->3->2->1.

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

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 the worst-case time complexity of Merge Sort is O(nLogn) and Insertion sort is O(n2), merge sort is preferred.

This set of Data Structure Questions and Answers for Experienced people focuses on “Singly Linked Lists Operations – 3”.

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

```/* 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.

Explanation: *head_ref = prev; At the end of the while loop, the prev pointer points to the last node of the original linked list.
We need to change *head_ref so that the head pointer now starts pointing to the last node.

10. What is the output of the following function for start pointing to first node of following linked list?

```1->2->3->4->5->6
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);
}```

a) 1 4 6 6 4 1
b) 1 3 5 1 3 5
c) 1 2 3 5
d) 1 3 5 5 3 1

Explanation: fun() prints alternate nodes of the given Linked List, first from head to end, and then from end to the head.
If Linked List has an even number of nodes, then skips the last node.

11. The following C function takes a simply-linked list as an 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.

```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;
}
_______________________________
}```

c) head = p; p->next = q; q->next = NULL;

Explanation: When while loop completes its execution, node ‘p’ refers to the last node whereas the ‘q’ node refers to the node before ‘p’ in the linked list. q->next=NULL makes q as the last node. p->next=head places p as the first node. the head must be modified to ‘p’ as ‘p’ is the starting node of the list (head=p). Thus the sequence of steps are q->next=NULL, p->next=head, head=p.

12. The following C 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?

```struct node
{
int value;
struct node *next;
};
void rearrange(struct node *list)
{
struct node *p, * q;
int temp;
if ((!list) || !list->next)
return;
p = list;
q = list->next;
while(q)
{
temp = p->value;
p->value = q->value;
q->value = temp;
p = q->next;
q = p?p->next:0;
}
}```

a) 1, 2, 3, 4, 5, 6, 7
b) 2, 1, 4, 3, 6, 5, 7
c) 1, 3, 2, 5, 4, 7, 6
d) 2, 3, 4, 5, 6, 7, 1

Enroll Now

Explanation: The function rearrange() exchanges data of every node with its next node. It starts exchanging data from the first node itself.

13. In the worst case, the number of comparisons needed to search a singly linked list of length n for a given element is?
a) log 2 n
b) n2
c) log 2 n – 1
d) n

Explanation: In the worst case, the element to be searched has to be compared with all elements of the linked list.

14. Given a pointer to a node X in a singly linked list. Only one point is given, pointer to head node is not given, can we delete node X from the given linked list?
a) Possible if X is not the last node
b) Possible if the size of the linked list is even
c) Possible if the size of the linked list is odd
d) Possible if X is not the first node

Explanation: Following are simple steps.

```    struct node *temp  = X->next;
X->data  = temp->data;
X->next  = temp->next;
free(temp);```

15. You are given pointers to the first and last nodes of a singly linked list, which of the following operations are dependent on the length of the linked list?
a) Delete the first element
b) Insert a new element as a first element
c) Delete the last element of the list
d) Add a new element at the end of the list