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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智慧校园建设方案旨在通过先进的信息技术,为师生提供一个全面智能的感知环境和综合信息服务平台。该方案正处在从信息化第二阶段向第三阶段过渡的关键时期,致力于实现校园服务和管理的全面智能化。 方案的核心目标是构建一个集成的校园地理信息服务平台,通过该平台实现资产管理、房产信息管理、基础设施管理、绿化管理和能源监测管理等功能。同时,该平台将提供校园漫游、信息服务、指引服务、活动通知、用房服务和客流统计等多样化服务,以促进校园的和谐、绿色、平安和便捷。 在技术层面,智慧校园建设方案强调系统集成能力、数据统一分析能力、系统资源共享能力以及大数据集成处理能力。通过这些能力,可以构建统一的校园地理信息平台,提供综合的应用支撑和管理能力,实现系统平滑演进。 应用方向上,智慧校园建设方案围绕和谐校园、绿色校园、平安校园和掌上校园四个维度展开。和谐校园侧重于提供校园漫游、信息服务、指引服务等,增强师生的校园体验。绿色校园则关注资产管理和能源监测,推动校园的可持续发展。平安校园通过视频监控、数字巡更等手段,确保校园安全。掌上校园则利用移动设备,实现校园服务的随时随地访问。 最终,智慧校园建设方案将通过三维虚拟校史馆、720度成像技术等创新应用,提供身临其境的校园漫游体验,同时通过可视化管理和数据分析,优化校园资源配置和运营效率,实现校园管理的智能化和现代化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值