Last Updated : 17 Dec, 2018

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

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

Node *move(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;
}

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

What does the following function return.

(A) It reverses given linked list

(B) It moves the last element to the front of the list and return modified list

(C) It reverse the every two element of the linked list and return the modified list

(D) it does not modify and return same list


Answer: (B)

Explanation: Here a linked list is given as an argument to the function. If list is empty or contain single node then it do nothing.
p and q are two pointers taken and p=head.
While condition is used to traverse the linked list to reach to the end.
Make q=NULL, then last element removed
p->next=head, here p pointing to last element now point to the head
head=p, and head becomes p.

So, it moves the last element to the front of the list and return modified list.

Option (B) is correct.


Quiz of this Question


Share your thoughts in the comments