GATE CS 2010 · Arrays & Matrix Representation

MCQ · 2 marks · Problem PROG-DSTR-0166

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. 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;
}
Choose the correct alternative to replace the blank line.
  1. A
    q = NULL; p\(\rightarrow\)next = head; head = p;
  2. B
    q\(\rightarrow\)next = NULL; head = p; p\(\rightarrow\)next = head;
  3. C
    head = p; p\(\rightarrow\)next = q; q\(\rightarrow\)next = NULL;
  4. D
    q\(\rightarrow\)next = NULL; p\(\rightarrow\)next = head; head = p;

Solve it on GATE DA practice →More Basic Data Structures problems

Show the worked solutionOfficial GATE Answer Key: Option D.See full step-by-step discussion on GateOverflow: https://gateoverflow.in/2337/gate2010-36

GATE DA practice: past exam problems by chapter, with worked solutions · gatedaprep.com