708. Insert into a Cyclic Sorted List

发布时间:2019-07-17 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了708. Insert into a Cyclic Sorted List脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
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.

The following example may help you understand the problem better:

In the figure above, there is a cyclic sorted list of three elements.
You are given a reference to the node with value 3, and we need to
insert 2 into the list.

The new node should insert between node 1 and node 3. After the
insertion, the list should look like this, and we should still return
node 3.

思路

考虑三种case
case1 node.val < target < node.next.val
case2 node.val <= MinNode.val
case3 node.val >= MaxNode.val

暴力解法, 按照遍历的顺序, 如果插入数字比head小, 则遍历到数组开头的地方. 这时候要判断一下targe比最小值小的情况.
接下来就是从头遍历数组, 需要考虑的cornercase是target大于最大值的情况

复杂度

时间O(n) 空间O(1)

代码

class Solution {
    public Node insert(Node head, int insertVal) {
        Node target = new Node(insertVal);
        if (head == null) {
            target.next = target;
            return target;
        }
        Node p = head;
        if (insertVal < head.val) { //如果target比head的值小
            while (p.val < p.next.val) {
                p = p.next;
            }
            if (p.next.val >= insertVal) { //target比最小的数字还小
                insertNode(p, target);
                return head;
            }
            p = p.next;
        }
        while (p.val < insertVal && p.next.val < insertVal && p.next.val > p.val) {
            p = p.next;
        }
        if (p.next.val > p.val && p.next.val < insertVal) { //target在p和p.next之间
            insertNode(p.next, target);
            
        } else { //target大于最大值, 
            insertNode(p, target);
        }
        return head;
    }
    public void insertNode(Node head, Node target) {
        target.next = head.next;
        head.next = target;
    }
}

思路

另外一种简单的做法是用while循环遍历整个数组并找到合理的跳出条件, 个人觉得这种方法更加简单一些

代码

class Solution {
    public Node insert(Node head, int insertVal) {
        Node target = new Node(insertVal);
        if (head == null) {
            target.next = target;
            return target;
        }
        Node p = head.next;
        while (p != head) {
            if (p.val < p.next.val) {
                if (p.val <= insertVal && p.next.val >= insertVal) {
                    insertNode(p, target);
                    return head;
                }
                
            } else if (p.val > p.next.val) {
                if (insertVal > p.val || insertVal < p.next.val) {
                    insertNode(p, target);
                    return head;
                }
            } else {
                if (p.val == insertVal) {
                    insertNode(p, target);
                    return head;
                }
            }
            p = p.next;
        }
        //跳出, 说明所有的node的值都相同
        insertNode(p, target);
        return head;
    }
    public void insertNode(Node head, Node target) {
        target.next = head.next;
        head.next = target;
    }
}

脚本宝典总结

以上是脚本宝典为你收集整理的708. Insert into a Cyclic Sorted List全部内容,希望文章能够帮你解决708. Insert into a Cyclic Sorted List所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: