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;
    }
}

你可能感兴趣的:(leetcode,链表,linkedlist)