剑指 Offer II 029. 排序的循环链表
2022-06-07 本文已影响0人
邦_
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
}