# Data Structures | Linked List | 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 *move_to_front(Node *head) {  Node *p, *q;  if ((head == NULL: || (head->next == NULL))     return head;  q = NULL; p = head;  while (p-> next !=NULL)   {    q = p;    p = p->next;  }  _______________________________  return head;}

(A)

(B)

(C)

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

(D)

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.

Quiz of this Question
Please comment below if you find anything wrong in the above post

Previous
Next