Use one real linked list node per occurrence, with a private reference to the first:
class Node(val value: Int, var next: Node)

// Inside the queue implementation:
private var first: Node = null
The representation invariant says that the reachable chain is finite, acyclic and nondecreasing in value, and is owned by the queue. Equal adjacent values are explicitly allowed. The abstraction function is the multiset containing one occurrence of v.value for every reachable real linked list node v. Initially first==null represents the empty multiset. No artificial smallest Int is needed as a sentinel, and Int.MinValue remains a valid real value.

Articles by others on the same topic (0)

There are currently no matching articles.