The following C function takes a singly-linked list as input argument. It…

GATE · 2010 · CS · Computer Science & IT

The following C function takes a singly-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->next = head; head = p;

  2. B.

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

  3. C.

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

  4. D.

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

Attempted by 278 students.

Sign up free to check your answer

Sign up free

Explore the full course: Gate Guidance By Sanchit Sir

Loading lesson…