This question ask me to Insert a node into a Cyclic Sorted List (also in the Explore, linkedlist, conclution chapter)
Given a node from a cyclic linked list which is sorted in ascending order, write a function to insert a value into the list such that it remains a cyclic sorted list. The given node can be a reference to any single node in the list, and may not be necessarily the smallest value in the cyclic list.
If there are multiple suitable places for insertion, you may choose any place to insert the new value. After the insertion, the cyclic list should remain sorted.
If the list is empty (i.e., given node is null), you should create a new single cyclic list and return the reference to that single node. Otherwise, you should return the original given node.
And my code is :
if not head:
node = Node(insertVal, None)
node.next = node
return node
tmp = head
while tmp.next != head:
if tmp.val == insertVal or tmp.val <= insertVal <= tmp.next.val or (tmp.val > tmp.next.val and (insertVal <= tmp.next.val) or (insertVal >= tmp.val)):
new = Node(insertVal, None)
nxt = tmp.next
tmp.next = new
new.next = nxt
return head
tmp = tmp.next
new = Node(insertVal, None)
nxt = tmp.next
tmp.next = new
new.next = nxt
return headthanks in advanced