Open In App
Related Articles

Data Structures | Linked List | Question 7

Like Article
Save Article
Report issue

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. 


typedef struct node
  int value;
  struct node *next;
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;


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


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


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


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

Answer: (D)


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

Last Updated : 28 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Similar Reads