Data Structures | Linked List | Question 7

Last Updated :
Discuss
Comments

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 that contain the correct pseudocode for the blank line. 

C++
#include <iostream> struct Node {     int value;     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; } 
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; } 
Java
class Node {     int value;     Node next;     Node(int value) {         this.value = value;         this.next = null;     } }  Node moveToFront(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; } 
Python
class Node:     def __init__(self, value):         self.value = value         self.next = None   def move_to_front(head):     if head is None or head.next is None:         return head     p = head     q = None     while p.next is not None:         q = p         p = p.next     ______________________________     return head 
JavaScript
class Node {     constructor(value) {         this.value = value;         this.next = null;     } }  function moveToFront(head) {     if (head === null || head.next === null)         return head;     let p = head;     let q = null;     while (p.next !== null) {         q = p;         p = p.next;     }    _____________________________     return head; } 

q = NULL; next of p = head; head = p;

next of q = NULL; head = p; next of p = head;

head = p; next of p = q; next of q = NULL;

next of q = NULL; next of p = head; head = p;

Share your thoughts in the comments