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.node

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:ui

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.this

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.code

思路

考慮三種case
case1 node.val < target < node.next.val
case2 node.val <= MinNode.val
case3 node.val >= MaxNode.valthree

暴力解法, 按照遍歷的順序, 若是插入數字比head小, 則遍歷到數組開頭的地方. 這時候要判斷一下targe比最小值小的狀況.
接下來就是從頭遍歷數組, 須要考慮的cornercase是target大於最大值的狀況ip

複雜度

時間O(n) 空間O(1)element

代碼

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循環遍歷整個數組並找到合理的跳出條件, 我的以爲這種方法更加簡單一些rem

代碼

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;
    }
}
相關文章
相關標籤/搜索