Data Structures | Linked List | Question 7
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.
Node *move_to_front(Node *head)
Node *p, *q;
((head == NULL: || (head->next == NULL))
q = NULL; p = head;
(p-> next !=NULL)
q = p;
p = p->next;
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;
To move the last element to the front of the list, we need to do the following steps:
- Make the second last node as the last node (i.e., set its next pointer to NULL).
- Set the next pointer of the current last node (which is now the second last node) to the original head node.
- 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