Insert into a Sorted Circular Linked List - Problem
Insert into a Sorted Circular Linked List

You're working with a circular linked list that maintains elements in non-descending sorted order. Your task is to insert a new value insertVal into this list while preserving its sorted property.

What makes this tricky?
• The list is circular - the last node points back to the first
• You're given a reference to any node in the list (not necessarily the smallest)
• You need to find the correct insertion point to maintain sorted order
• Special case: if the list is empty (null), create a new single-node circular list

Goal: Insert the value and return the originally given node reference (or the new node if list was empty).

Key Challenge: There are three main scenarios to handle:
1. Insert between two nodes where prev.val ≤ insertVal ≤ next.val
2. Insert at the boundary (smallest or largest position)
3. All nodes have the same value

Input & Output

example_1.py — Normal Insertion
$ Input: head = [3,4,1], insertVal = 2
Output: [3,4,1,2] (circular: 3→4→1→2→3→...)
💡 Note: The value 2 fits perfectly between 1 and 3. We find the position where 1 ≤ 2 ≤ 3 and insert there. The list remains sorted in circular fashion.
example_2.py — Boundary Insertion
$ Input: head = [1,3,5], insertVal = 6
Output: [1,3,5,6] (circular: 1→3→5→6→1→...)
💡 Note: The value 6 is larger than all existing values, so it should be inserted at the 'boundary' - between the largest value (5) and smallest value (1) in the circular arrangement.
example_3.py — Empty List
$ Input: head = null, insertVal = 1
Output: [1] (circular: 1→1→1→...)
💡 Note: When the list is empty, we create a new node that points to itself, forming a single-node circular list.

Visualization

Tap to expand
Case 1: Normal (2 between 1,3)132Case 2: Boundary (6 after max 5)156Case 3: Same Values (any position)3333Key Patterns:• Normal: prev ≤ val ≤ next• Boundary: prev > next - val ≥ prev OR val ≤ next• Same: insert anywhere
Understanding the Visualization
1
Normal Case
Insert when prev.val ≤ insertVal ≤ next.val
2
Boundary Case
Insert at wrap-around when insertVal is largest or smallest
3
Same Values
Insert anywhere when all nodes have identical values
Key Takeaway
🎯 Key Insight: A sorted circular list has at most one 'break point' where large values wrap to small values. This is where boundary insertions occur!

Time & Space Complexity

Time Complexity
⏱️
O(n)

Single traversal through the circular list, visiting each node at most once

n
2n
Linear Growth
Space Complexity
O(1)

Only using a constant amount of extra space for pointers

n
2n
Linear Space

Constraints

  • The number of nodes in the list is in the range [0, 5 × 104]
  • -106 ≤ Node.val ≤ 106
  • -106 ≤ insertVal ≤ 106
  • The list is guaranteed to be sorted in non-descending order
Asked in
Facebook 45 Google 38 Microsoft 32 Amazon 28
89.0K Views
Medium-High Frequency
~18 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen