Question 26

Computer Awareness Linked Lists Medium

The statements of pseudocode for searching the first element with key k in the linked list L are given below. Arrange them in the correct order $$ \text{ (A) while ($x \ne$ NIL and $x.key \ne k$) } $$ $$ \text{ (B) $x=L.head$ } $$ $$ \text{ (C) $x = x.next$ } $$ $$ \text{ (D) return $x$ } $$

(A) (A), (C), (B), (D)
(B) (A), (B), (C), (D)
(C) (B), (A), (C), (D)
(D) (B), (C), (A), (D)
View Dynamic Solution & Explanation
Correct Solution: Option C

Step-by-step Solution:

Solution:

We are asked to arrange the pseudocode steps to search for the first element with key k in a linked list L.

Step-wise reasoning:

  1. (B) x = L.head → Start by setting x to the head of the list.
  2. (A) while (x ≠ NIL and x.key ≠ k) → Traverse the list until either the key is found or the list ends.
  3. (C) x = x.next → Move to the next node during traversal.
  4. (D) return x → Once the loop ends, return x. If found, x is the node with key k; otherwise, NIL.

✅ Correct Order: (B), (A), (C), (D)
✅ Correct Answer: (C)