Question: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. 
(A) while (x != NIL and x.key != k) 
(B) x = L.head 
(C) x = x.next 
(D) return x 

Show Hint

The search algorithm in a linked list starts from the head, checks for the key, moves to the next node, and returns the node once the element is found.
Updated On: Mar 7, 2026
  • (B), (A), (C), (D)
  • (A), (B), (C), (D)
  • (C), (B), (A), (D)
  • (C), (B), (D), (A)
Show Solution

The Correct Option is A

Solution and Explanation

Step 1: Pseudocode Analysis for Linked List Search.
The process for searching an element in a linked list can be outlined as follows: 

- (B) Initialize a pointer `x` to the head of the linked list, denoted as `L.head`. 

- (A) Employ a while loop. This loop continues as long as the current node `x` is not `NIL` (the end of the list) AND the key of the current node (`x.key`) does not match the target key `k`. 

- (C) Advance the pointer to the next node in the list by assigning `x = x.next`. 

- (D) Upon locating the node containing the target key, return the node `x`.

Step 2: Sequence Determination. 
The logically correct execution order for these steps is (B), (A), (C), (D). This sequence ensures a systematic search commencing from the list's beginning, proceeding node by node, and concluding either upon finding the element or reaching the list's end. 
 

Was this answer helpful?
0