Home › Previous year questions › GATE 2010 CS (CS1) › Question 46GATE 2010 CS (CS1) – Question 46 Programming and Data Structures · Linked Lists · 2 marks · Multiple choice
For a singly linked list with at least two nodes, fill the blank to move its last node to the front.
Node *move(Node *head) {
Node *p=head, *q=NULL;
while (p->next != NULL) { q=p; p=p->next; }
/* blank */
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; Practise this question in The GATE Grind →
Show answer and explanation Correct answer: (D) q->next=NULL; p->next=head; head=p;
Explanation At loop exit p points to the last node and q to its predecessor. First terminate the shortened list with q->next=NULL. Link the old last node to the old head using p->next=head, then update head=p. This is D; changing head before linking would lose the original front or create a self-loop.
More Linked Lists questions GATE 2021 CS (CS2) Q45 – Which statement about this C program is correct? GATE 2026 CS (CS1) Q39 – Consider the following code snippet in C language that computes the number of nodes in a… GATE 2025 CS (CS1) Q62 – Let LIST be a datatype for an implementation of linked list defined as follows: Suppose a… GATE 2023 CS Q13 – Let SLLdel be a function that deletes a node in a singly-linked list given a pointer to… GATE 2022 CS Q15 – Consider the problem of reversing a singly linked list. To take an example, given the… GATE 2020 CS Q26 – What is the worst case time complexity of inserting n elements into an empty linked list,… All Linked Lists questions →