leetcode-链表篇3

news/2024/10/3 16:21:37 标签: leetcode, 链表, 算法

leetcode-61

给你一个链表的头节点 head ,旋转链表,将链表每个节点向右移动 k 个位置。

示例 1:

leetcode.com%2Fuploads%2F2020%2F11%2F13%2Frotate1.jpg&pos_id=cLotG7Yi" width="534" />

输入:head = [1,2,3,4,5], k = 2
输出:[4,5,1,2,3]

示例 2:

输入:head = [0,1,2], k = 4
输出:[2,0,1]

提示:

  • 链表中节点的数目在范围 [0, 500] 内
  • -100 <= Node.val <= 100
  • 0 <= k <= 2 * 109

思路:看图片可以发现,这个题目本质上,是把链表分成两部分,交换前后两部分的顺序

所以重点在于找到分裂点,以及拼接

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* rotateRight(ListNode* head, int k) {
        if(!head || !head->next || k == 0) {
            return head;
        }
        auto tmp = head;
        int count = 0;
        auto tail = head;
        while(tmp) {
            tail = tmp;
            tmp = tmp->next;
            ++count;
        }
        tmp = head;
        k %= count;
        if(k == 0) {
            return head;
        }
        k = count - k;
        while(--k && tmp) {
            tmp = tmp->next;
        }
        auto new_head = tmp->next;
        tmp->next = nullptr;
        tail->next = head;
        return new_head;
    }   
};

leetcode-206

给你单链表的头节点 head ,请你反转链表,并返回反转后的链表

示例 1:

leetcode.com%2Fuploads%2F2021%2F02%2F19%2Frev1ex1.jpg&pos_id=cxsJkjIs" width="534" />

输入:head = [1,2,3,4,5]
输出:[5,4,3,2,1]

示例 2:

leetcode.com%2Fuploads%2F2021%2F02%2F19%2Frev1ex2.jpg&pos_id=hN0iV2D1" width="534" />

输入:head = [1,2]
输出:[2,1]

示例 3:

输入:head = []
输出:[]

提示:

  • 链表中节点的数目范围是 [0, 5000]
  • -5000 <= Node.val <= 5000

进阶:链表可以选用迭代或递归方式完成反转。你能否用两种方法解决这道题?

思路:递归法比较简单,还是我们的老模板

void circle(ListNode *head) {

    if(...) {
        return 
    }        //边界条件

    auto res = circle(head->next);
    
    //other code
    //return
}

迭代法也是一器破万法。

关于链表反转的题目,都分成两部分,pre和curr

pre就是已经处理好的部分的尾节点, curr就是你要处理的部分的头节点

所以伪代码如下

//关于翻转链表相关的题目
while(xxx) {
    process(curr);      //实现翻转的逻辑
    pre = curr;         //当前部分已经处过,所以pre变成curr
    curr = curr->next;  //curr->next还没处理过,所以curr指向自己的next继续处理
}
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        if(!head || !head->next) {
            return head;
        }
        // auto next = reverseList(head->next);
        // auto tmp = head->next;
        // head->next = nullptr;
        // tmp->next = head;
        // return next;
        auto pre = head;
        pre = nullptr;
        while(head) {
            auto next = head->next;
            head->next = nullptr;
            head->next = pre;
            pre = head;
            head = next;
        }
        return pre;
    }
};

leetcode-92

给你单链表的头指针 head 和两个整数 left 和 right ,其中 left <= right 。请你反转从位置 left 到位置 right 的链表节点,返回 反转后的链表 。

示例 1:

leetcode.com%2Fuploads%2F2021%2F02%2F19%2Frev2ex2.jpg&pos_id=OOBppl2a" width="534" />

输入:head = [1,2,3,4,5], left = 2, right = 4
输出:[1,4,3,2,5]

示例 2:

输入:head = [5], left = 1, right = 1
输出:[5]

提示:

  • 链表中节点数目为 n
  • 1 <= n <= 500
  • -500 <= Node.val <= 500
  • 1 <= left <= right <= n

进阶: 你可以使用一趟扫描完成反转吗?

思路,和上题一样,不过是对翻转的区间有限制了。

加上前缀节点pre,找到要翻转的区间的头尾节点,将之翻转后。拼接回原来的链表

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:




    ListNode* reverseBetween(ListNode* head, int left, int right) {
        if(!head || !head->next) {
            return head;
        }
        auto tmp = new ListNode(-1);
        tmp->next = head;
        auto pre = tmp;
        int count = left;
        while(--count) {
            tmp = tmp->next;
        }
        auto curr = tmp->next;
        for(int i=0; i<right-left; i++) {
            auto tail = curr->next->next;
            auto next = curr->next;
            curr->next = tail;

            auto new_tail = tmp->next;
            tmp->next = next;
            next->next = new_tail;
        }
        return pre->next;
    }
};

leetcode-24

给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换)。

示例 1:

leetcode.com%2Fuploads%2F2020%2F10%2F03%2Fswap_ex1.jpg&pos_id=gllGTNzY" width="534" />

输入:head = [1,2,3,4]
输出:[2,1,4,3]

示例 2:

输入:head = []
输出:[]

示例 3:

输入:head = [1]
输出:[1]

提示:

  • 链表中节点的数目在范围 [0, 100] 内
  • 0 <= Node.val <= 100

思路:还记得我们一器破万法的迭代法模板吗,直接套用就可以

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:

    ListNode* swapPairs(ListNode* head) {
        if(!head || !head->next) {
            return head;
        }
        auto pre = new ListNode(-1);
        pre->next = head;
        auto curr = head;
        auto tmp = pre;
        while(curr && curr->next) {
            auto tail = curr->next->next;
            auto next = curr->next;
            curr->next = tail;

            auto new_tail = tmp->next;
            next->next = new_tail;
            tmp->next = next;

            tmp = curr;
            curr = curr->next;
        }
        return pre->next;
    }
};

leetcode-25

给你链表的头节点 head ,每 k 个节点一组进行翻转,请你返回修改后的链表

k 是一个正整数,它的值小于或等于链表的长度。如果节点总数不是 k 的整数倍,那么请将最后剩余的节点保持原有顺序。

你不能只是单纯的改变节点内部的值,而是需要实际进行节点交换。

示例 1:

输入:head = [1,2,3,4,5], k = 2
输出:[2,1,4,3,5]

示例 2:

leetcode.com%2Fuploads%2F2020%2F10%2F03%2Freverse_ex2.jpg&pos_id=KgOPaJF4" width="534" />

输入:head = [1,2,3,4,5], k = 3
输出:[3,2,1,4,5]

提示:
  • 链表中的节点数目为 n
  • 1 <= k <= n <= 5000
  • 0 <= Node.val <= 1000

进阶:你可以设计一个只用 O(1) 额外内存空间的算法解决此问题吗?

思路:做了以上几道题目,总结了自己的一器破万法模板之后,发现这个题竟然这么简单

以前被自己视为面试杀手的接雨水,k个一组翻转链表也变得简单起来

这题和上一题的唯一不同就在于,需要遍历一遍算出链表的长度,避免越界

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* reverseKGroup(ListNode* head, int k) {
        auto tmp = head;
        int count=0;
        while(tmp) {
            ++count;
            tmp = tmp->next;
        }
        if(!head || !head->next || k>count || k<=1) {
            return head;
        }
        auto pre = new ListNode(-1);
        pre->next = head;
        tmp = pre;
        auto curr = head;    
        for(int i=0; i<count/k; i++) {
            for(int j=1; j<k; j++) {
                auto tail = curr->next->next;
                auto next = curr->next;
                curr->next = tail;

                auto new_tail = tmp->next;
                next->next = new_tail;
                tmp->next = next;
            }
            tmp = curr;
            curr = curr->next;
        }
        return pre->next;
    }
};


http://www.niftyadmin.cn/n/5688654.html

相关文章

《PMI-PBA认证与商业分析实战精析》第5章 需求启发与分析

第5章 需求启发与分析 本章主要内容&#xff1a; 需求启发 需求分析 模型化与优化需求 记录、确认、核实和批准需求 本章涵盖的考试重点&#xff1a; 需求启发的四项活动 需求启发的八项技术 启发提问的四种类型问题 启发原型的类型 访谈的四种分类 观察技术的四种类…

【STM32开发环境搭建】-4-在STM32CubeMX中新增Keil(MDK-ARM) 5的工程目录(包含指定路径的C和H文件)

案例背景&#xff1a; 由于Keil(MDK-ARM)5工程&#xff1a;DEMO_STM32F030C8T6.uvprojx是由STM32CubeMX工具生成的&#xff0c;如果我们在Keil工程中手动添加了一些c文件和h文件的Include Path包含路径&#xff0c;会在STM32CubeMX下一次生成uvprojx文件时&#xff0c;被删除&…

【Android 源码分析】Activity生命周期之onDestroy

忽然有一天&#xff0c;我想要做一件事&#xff1a;去代码中去验证那些曾经被“灌输”的理论。                                                                                  – 服装…

如何在 Windows 10 上恢复未保存/删除的 Word 文档

您是否整夜都在处理重要的 word 文件&#xff0c;但忘记保存它&#xff1f;这篇文章是给你的。在这里&#xff0c;我们将解释如何恢复未保存的 word 文档。除此之外&#xff0c;您还将学习如何恢复已删除的 word 文档。 从专业人士到高中生&#xff0c;每个人都了解丢失重要 W…

GO网络编程(一):基础知识

1. 网络编程的基础概念 TCP/IP 协议栈 TCP/IP 是互联网通信的核心协议栈&#xff0c;分为以下四个层次&#xff1a; 应用层&#xff08;Application Layer&#xff09;&#xff1a;为应用程序提供网络服务的协议&#xff0c;比如 HTTP、FTP、SMTP 等。传输层&#xff08;Tra…

网上客车售票管理系统(含源码+sql+视频导入教程+文档+PPT)

&#x1f449;文末查看项目功能视频演示获取源码sql脚本视频导入教程视频 1 、功能描述 网上客车售票管理系统4拥有两种角色&#xff1a;管理员和用户 管理员&#xff1a;车票管理、订单管理、退票管理、车票流水记录、余票盘点、留言管理、用户管理等 用户&#xff1a;登录…

构建electron项目

1. 使用electron-vite构建工具 官网链接 安装构建工具 pnpm i electron-vite -g创建electron-vite项目 pnpm create quick-start/electron安装所有依赖 pnpm i其他 pnpm -D add sass scss1. 启动项目 2. 配置 package.json "dev": "electron-vite dev --…

MATLAB计算与建模常见函数:4.插值

插值 什么是插值&#xff1f; 通常实验测量或者采集的数据都是离散数值&#xff1b;插值是指在所给的基准数据情况下&#xff0c;研究如何平滑地估算出基准数据之间其它点的函数数值&#xff1b;一些点的数据无法获得&#xff0c;或者获取这些点的数据代价较高时&#xff0c;…