func insert(_ head: Node?, _ insertVal: Int) -> Node? {
if head == nil {
let node = Node(insertVal)
node.next = node
return node
}
var tempHead = head
while tempHead?.next !== head {
let tempValue = tempHead?.val ?? 0
let tempNextValue = tempHead?.next?.val ?? 0
//到达临界点
if tempValue > tempNextValue {
//大于最大值或者小于最小值
if tempValue < insertVal || insertVal < tempNextValue {
break
}
}
//满足条件的插入点
if (tempValue <= insertVal) && (insertVal <= tempNextValue) {
break
}
tempHead = tempHead?.next
}
let insertNode = Node(insertVal)
insertNode.next = tempHead?.next
tempHead?.next = insertNode
return head
}
网友评论