Insert into a Cyclic Sorted List
Question (LI.599)
Given a random node from a sorted circular linked list, insert a new value into a list and maintain the sorted property. Return the new node.
Node definition? Positive values only? No. Allow duplicates? Yes.
Example
I: 3->5->1->, 4
O: 3->4->5->1->
I: null, 5
O: 5->
I: 5->, 6
O: 5->6->
I: 5->6->1->, 0
O: 5->6->0->1->Analysis
Normal case: insert when
Corner case: insert before start when new_val is min or max
Code
Note: It is possible to write the solution in one iteration. Then the code has to check for the min/max case explicitly.
Last updated