Insert into a Sorted Circular Linked List — LeetCode 708 Python Solution

MediumLeetCode PremiumLinked List
Problem
#708
Reading time
5 min

The problem

Given a Circular Linked List node, which is sorted in non-descending order, write a function to insert a value insertVal into the list such that it remains a sorted circular list. The given node can be a reference to any single node in the list and may not necessarily be the smallest value in the circular list.

Example

Input
head = [3,4,1], insertVal = 2
Output
[3,4,1,2]
Explanation
In the figure above, there is a sorted circular list of three elements. You are given a reference to the node with value 3, and we need to insert 2 into the list. The new node should be inserted between node 1 and node 3. After the insertion, the list should look like this, and we should still return node 3.

Python solution

Python
"""
# Definition for a Node.
class Node:
    def __init__(self, val=None, next=None):
        self.val = val
        self.next = next
"""


class Solution:
    def insert(self, head: 'Optional[Node]', insertVal: int) -> 'Node':
        node = Node(insertVal)
        if head is None:
            node.next = node
            return node
        prev, curr = head, head.next
        while curr != head:
            if prev.val <= insertVal <= curr.val or (
                prev.val > curr.val and (insertVal >= prev.val or insertVal <= curr.val)
            ):
                break
            prev, curr = curr, curr.next
        prev.next = node
        node.next = curr
        return head

Complexity

MeasureComplexity
TimeO(n)
SpaceO(1) auxiliary

Pattern: Linked List

Rewire pointers in place, with a dummy head and a saved next to keep it safe. LeetCode 708. Insert into a Sorted Circular Linked List is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Linked List.

The linked list guide has the Python template for the pattern and the 75 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 708. Insert into a Sorted Circular Linked List?
LeetCode 708. Insert into a Sorted Circular Linked List is rated Medium on LeetCode.
What is the time complexity of LeetCode 708. Insert into a Sorted Circular Linked List?
The Python solution on this page runs in O(n).
What is the space complexity of LeetCode 708. Insert into a Sorted Circular Linked List?
The Python solution on this page uses O(1) auxiliary space.
What topics does LeetCode 708. Insert into a Sorted Circular Linked List cover?
LeetCode 708. Insert into a Sorted Circular Linked List is tagged Linked List on LeetCode.
Is LeetCode 708. Insert into a Sorted Circular Linked List a premium problem?
Yes. LeetCode 708. Insert into a Sorted Circular Linked List is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview