美文网首页
剑指 Offer II 029. 排序的循环链表

剑指 Offer II 029. 排序的循环链表

作者: 邦_ | 来源:发表于2022-06-07 17:30 被阅读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
        
        }
    
    

    相关文章

      网友评论

          本文标题:剑指 Offer II 029. 排序的循环链表

          本文链接:https://www.haomeiwen.com/subject/dljdertx.html