Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Data Structures | Linked List | Question 7

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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)

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

(B)

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

(C)

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

(D)

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


Answer: (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

My Personal Notes arrow_drop_up
Last Updated : 28 Jun, 2021
Like Article
Save Article
Similar Reads
Related Tutorials