Data Structures & AlgorithmsModerate

A sorted linked list holds 1 2 2 3 3 3 4 4 4 4. Delete nodes so that each value keeps AT MOST TWO nodes, preserving order. How many nodes remain?

  1. A. 4
  2. B. 6
  3. C. 7
  4. D. 9

Correct answer: C. 7

Walk the list once, keeping a node when it differs from the node two places before it in the kept list. Counting distinct values or subtracting the largest group ignores the keep-two rule.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic