/**
* Definition for a Node.
* type Node struct {
* Val int
* Next *Node
* }
*/
func insert(head *Node, insertVal int) *Node {
node := &Node{Val: insertVal}
if head == nil {
node.Next = node
return node
}
if head.Next == head {
head.Next = node
node.Next = head
return head
}
curr, next := head, head.Next
for next != head {
if insertVal >= curr.Val && insertVal <= next.Val {
break
}
if curr.Val > next.Val {
if insertVal > curr.Val || insertVal < next.Val {
break
}
}
curr = curr.Next
next = next.Next
}
curr.Next = node
node.Next = next
return head
}
"""
# 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: 'Node', insertVal: int) -> 'Node':
node = Node(insertVal)
if head is None:
node.next = node
return node
if head.next == head:
head.next = node
node.next = head
return head
curr = head
next = head.next
while next != head:
if curr.val <= insertVal <= next.val:
break
if curr.val > next.val:
if insertVal > curr.val or insertVal < next.val:
break
curr = curr.next
next = next.next
curr.next = node
node.next = next
return head