【遍历链表】个人练习-Leetcode-LCR 029. 循环有序列表的插入

题目链接:https://leetcode.cn/problems/4ueAj6/description/

题目大意:给出一个循环链表中间的某个结点的指针head(这个并非真正的头),这个链表从头到尾是非递减的,唯一可能出现递减的地方是【尾部连回头部】处。现在给一个值insertVal,要求将该值插入链表中,保持非递减性质不变。返回的还是原来的head指针。

思路:(1)先考虑正常的从头到尾非递减的情况,如果插入值val与某个结点值nowv相同,那么直接插到其后面就行。否则无论是大了还是小了,都得再往后递归。

(2)如果刚好碰到尾部接头部处,那么如果val比尾部值大或者比头部值小,都可以直接插在尾部值后。否则往后递归。

(3)有一种特殊情况是全链表的元素相同。此时我们无法找到(1)(2)中所谓的【尾部接到头部】处(因为不存在nowv > nxtv的情况了),因此单独做判断。这这种情况,val插到任意处都行。

完整代码

cpp 复制代码
class Solution {
public:
    void inop(Node* nd, int val, Node* res) {
        int nowv = nd->val;
        if (val == nowv) {
            Node* tmp = nd->next;
            nd->next = res;
            res->next = tmp;
            return;
        }
        int nxtv = nd->next->val;
        if (nowv == nxtv) 
            inop(nd->next, val, res);
        else if (nowv < nxtv) {
            if (val < nowv)
                inop(nd->next, val, res);
            else { // val > nowv
                if (val <= nxtv) {
                    res->next = nd->next;
                    nd->next = res;
                    return;
                }
                else
                    inop(nd->next, val, res);
            }
        }
        else { // nowv > nxtv, final node
            if (val >= nowv) {
                res->next = nd->next;
                nd->next = res;
                return;
            }
            else { // val < nowv
                if (val <= nxtv) {
                    res->next = nd->next;
                    nd->next = res;
                    return;
                }
                else // val > nxtv
                    inop(nd->next, val, res);
            }
        }
    }

    Node* insert(Node* head, int insertVal) {
        Node* res = new Node(insertVal); 
        if (head == nullptr) {
            res->next = res;
            return res;
        }

        Node* temp = head->next;
        int headv = head->val;
        bool flag = true;
        while (temp != head) {
            if (temp->val != headv) {
                flag = false;
                break;
            }
            temp = temp->next;
        }    
        if (flag) {
            res->next = head->next;
            head->next = res;
            return head;
        }

        inop(head, insertVal, res);
        return head;
    }
};
相关推荐
KingRumn3 分钟前
DBUS源码剖析之DBusMessage数据结构
linux·服务器·数据结构
youngee1127 分钟前
hot100-61电话号码的字母组合
java·数据结构·leetcode
POLITE333 分钟前
Leetcode 76.最小覆盖子串 JavaScript (Day 6)
javascript·算法·leetcode
jackyrongvip35 分钟前
10个动画介绍递归(用Gemin3生成)
数据结构·递归·gemin3
一条大祥脚2 小时前
26.1.1
数据结构·算法
一起养小猫2 小时前
LeetCode100天Day6-回文数与加一
java·leetcode
POLITE32 小时前
Leetcode 234.回文链表 JavaScript (Day 9)
javascript·leetcode·链表
chushiyunen4 小时前
快慢双指针算法笔记
数据结构·笔记·算法
@小码农4 小时前
202512 电子学会 Scratch图形化编程等级考试三级真题(附答案)
服务器·开发语言·数据结构·数据库·算法