Skip to contentKEEP THE CRAFTTHE SANCTUARY

Insert Into a Sorted Circular List

Insert a value into a sorted circular list, including at the wrap from largest to smallest.

The problem

Implement Solution.insert(values, insertVal). A circular linked list is sorted in non-decreasing order, and the node after the largest value wraps around to the smallest. The list is given as values: one full trip around the circle, starting from its smallest value, so the last entry is followed by the first. Insert insertVal so the circle stays sorted, and return one full trip around the new circle, again starting from its smallest value. For example, values = [1, 3, 4] and insertVal = 2 gives [1, 2, 3, 4], and insertVal = 0 gives [0, 1, 3, 4]. If values is empty, the new circle holds only insertVal. Do not modify values. There are at most 50,000 values.

Starter code

class Solution:
    def insert(self, values, insertVal):
        pass

Public examples

  1. values = [1, 3, 4], insertVal = 2gives[1, 2, 3, 4]
  2. values = [1, 3, 4], insertVal = 8gives[1, 3, 4, 8]
  3. values = [2, 2, 4], insertVal = 2gives[2, 2, 2, 4]

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.