The problem. You're given a node from a circular singly linked list whose values are sorted in non-decreasing order when read round the ring — but the node you're given can be any node, not necessarily the smallest. Insert insertVal so the ring stays sorted, and return the node you were given. If the list is empty, create a one-node ring and return it.
ring from the given node: 3 -> 4 -> 1, insertVal = 2 -> 3 -> 4 -> 1 -> 2
ring: 3 -> 5 -> 1, insertVal = 6 -> 3 -> 5 -> 6 -> 1
ring: 3 -> 3 -> 3, insertVal = 4 -> 3 -> 3 -> 3 -> 4 (anywhere)
empty, insertVal = 1 -> 1 (pointing to itself)Members only · free account
The rest of this lesson, with 6 more sections, 1 drawing, a dry-run simulator and code in JavaScript, Python, Java and C++, opens with a free account.
Not ready yet? The Arrays course is free to read, no account needed.
In the rest of this lesson