GATE Computer ScienceVery hardGATE CS (PYQ pattern)

Keys 5, 3, 8, 3, 9, 1, 8, 5 are inserted one by one into an initially empty sorted singly linked list; each insertion scans from the head and stops at the first node whose value is not less than the new key (the node is inserted before it). The comparison of a key with the list's node values counts one per node examined, including the one that stops the scan. How many comparisons are made in total?

  1. A. 18
  2. B. 20
  3. C. 15
  4. D. 23

Correct answer: A. 18

For each insertion count the nodes with smaller value plus one stopping node (if any exists). Summing over the 8 insertions gives 18.

More Programming and Data Structures (GATE) questions

Exams that ask this topic