Open In App

GATE | Sudo GATE 2020 Mock I (27 December 2019) | Question 35

Like Article
Like
Save
Share
Report

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




typedef struct node {
  int value;
  struct node * next;
}
Node;
  
Node * modify_list(Node * head) {
  
    Node * p, * q;
    if ((head == NULL || (head-> next == NULL)) 
    return head; 
      
    q = NULL; 
    p = head;
      while (p-> next-> next != NULL) {
        q = p;
        p = p -> next;
      }
      q -> next = p -> next; 
      p -> next = head; 
      head = p;
        
      return head;
    }


It modifies the list
(A) by moving the last element to the front of the list and returns the modified list.
(B) by moving the second last element to the front of the list and returns the modified list.
(C) by moving the first element to the second last of the list and returns the modified list.
(D) by moving the first element to the last of the list and returns the modified list.


Answer: (B)

Explanation: Function modify_list(Node * head) modifies the list by moving the second last element to the front of the list and returns the modified list.

Option (B) is correct.

Quiz of this Question


Last Updated : 26 Dec, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads