【牛客刷题】链表专项 Ⅱ()

合并k个有序链表

https://www.nowcoder.com/share/jump/9321389651694169697706
递归调用一下合并2个有序链表就OK:

    ListNode* merge2Lists(ListNode* pHead1, ListNode* pHead2) {ListNode* dummy = new ListNode(0);ListNode* cur = dummy;ListNode* cur1 = pHead1;ListNode* cur2 = pHead2;while (cur1 != nullptr && cur2 != nullptr) {if (cur1->val <= cur2->val) {cur->next = cur1;cur1 = cur1->next;} else {cur->next = cur2;cur2 = cur2->next;}cur = cur->next;}if (cur1 == nullptr && cur2 != nullptr) cur->next = cur2;else if (cur2 == nullptr && cur1 != nullptr) cur->next = cur1;return dummy->next;}ListNode* mergeKLists(vector<ListNode*>& lists) {if (lists.size() == 0) return nullptr;;// write code hereint k = lists.size();ListNode* newList = merge2Lists(lists[0], lists[1]);for (int i = 2; i < k; i++) {newList = merge2Lists(newList, lists[i]);}return newList; }

单链表排序

题目没说不可以直接修改链表的值,所以用vector存值排序后直接改就行。。。

    ListNode* sortInList(ListNode* head) {// write code herevector<int> vals;ListNode* cur = head;while (cur != nullptr) {vals.push_back(cur->val);cur = cur->next;}cur = head;sort(vals.begin(), vals.end());for (int i = 0; i < vals.size(); i++) {cur->val = vals[i];cur = cur->next;}return head;}

其实一开始想到的是另一种,就是用小根堆把每个节点排一下序,然后构造个新链表每次都取小根堆堆顶的节点,再弹出更新:
这个写法也可以过,就是写优先级队列的时候语法不好记:
auto comp = [](ListNode* a, ListNode* b){return a->val > b->val;};
priority_queue<ListNode*, vector<ListNode*>, decltype(comp)> pq(comp);

auto comp = [](ListNode * a, ListNode * b) {return a->val > b->val;
};
priority_queue<ListNode*, vector<ListNode*>, decltype(comp)> nodeQue(comp);
ListNode* sortInList(ListNode* head) {// write code hereif (head == nullptr || head->next == nullptr) return head;auto comp = [](ListNode * a, ListNode * b) {return a->val > b->val;};priority_queue<ListNode*, vector<ListNode*>, decltype(comp)> nodeQue(comp);ListNode* cur = head;while (cur != nullptr) {nodeQue.push(cur);cur = cur->next;}ListNode* dummy = new ListNode(0);cur = dummy;while (!nodeQue.empty()) {cur->next = nodeQue.top();nodeQue.pop();cur = cur->next;}cur->next = nullptr;return dummy->next;}

判断链表是否回文

翻转一半链表:

    ListNode* reverse(ListNode* head) {ListNode* pre = nullptr;ListNode* cur = head;while (cur != nullptr) {ListNode* temp = cur->next;cur->next = pre;pre = cur;cur = temp;}return pre;}bool isPail(ListNode* head) {// write code hereif (head == nullptr || head->next == nullptr) return true;ListNode* fast = head;ListNode* slow = head;while (fast && fast->next) {fast = fast->next->next;slow = slow->next;}ListNode* halfNodes = reverse(slow);ListNode* cur = head;while (halfNodes) {if (cur->val != halfNodes->val) return false;cur = cur->next;halfNodes = halfNodes->next;}   return true;}

或者翻前翻后用一个vector存储当前的值再比较就行:

    ListNode* reverse(ListNode* head) {ListNode* pre = nullptr;ListNode* cur = head;while (cur != nullptr) {ListNode* temp = cur->next;cur->next = pre;pre = cur;cur = temp;}return pre;}bool isPail(ListNode* head) {// write code hereif (head == nullptr || head->next == nullptr) return true;ListNode* cur = head;vector<int> valList;while (cur) {valList.push_back(cur->val);cur = cur->next;}ListNode* pre = reverse(head);for (int i = 0; i < valList.size(); i++) {if (pre->val != valList[i]) return false;pre = pre->next;}return true;}

删除有序链表的重复元素

美团一面的题呜呜呜,这个题没做出来我是真该死啊

    ListNode* deleteDuplicates(ListNode* head) {// write code hereif (head == nullptr || head->next == nullptr) return head;ListNode* dummy = new ListNode(0);dummy->next = head;ListNode* cur = head;while (cur && cur->next) {if (cur->val == cur->next->val) cur->next = cur->next->next;else cur = cur->next;}return dummy->next;}

删除有序链表重复元素Ⅱ

用map存val出现次数,画图认真理,可以推出来的:

    ListNode* deleteDuplicates(ListNode* head) {// write code hereif (head == nullptr || head->next == nullptr) return head;ListNode* dummy = new ListNode(0);dummy->next = head;ListNode* cur = head;ListNode* pre = dummy;unordered_map<int, int> valMap;valMap[cur->val]++;while (cur && cur->next) {if (valMap[cur->next->val] == 1) {pre->next = cur->next->next;cur = pre;}else {valMap[cur->next->val]++;pre = cur;cur = cur->next;}}return dummy->next;}

链表奇偶重排

直接改值,比较简单的方法:

    ListNode* oddEvenList(ListNode* head) {// write code hereListNode* dummy = new ListNode(0);dummy->next = head;ListNode* cur = head;vector<int> valList;while (cur) {valList.push_back(cur->val);cur = cur->next;}cur = head;for (int i = 0; i < valList.size(); i += 2) {cur->val = valList[i];cur = cur->next;}for (int i = 1; i < valList.size(); i += 2) {cur->val = valList[i];cur = cur->next;}return dummy->next;}

一个比较巧妙的方法,开辟一个奇数节点链表和一个偶数节点链表,然后奇数尾连接偶数头:

    ListNode* oddEvenList(ListNode* head) {// write code hereif (!head || !head->next) return head;ListNode* odd = head;ListNode* even = head->next;ListNode* connect = even;//记录偶数节点的起点while (even && even->next) {//while (odd && odd->next)可能会导致遗漏一些偶数位置上的节点odd->next = even->next;odd = odd->next;even->next = odd->next;even = even ->next;}odd->next = connect;//奇偶相连return head;}

链表重排

https://www.nowcoder.com/share/jump/9321389651694178928216
用栈实现,只要每次cur移动的时候取一下栈顶元素就行:

    void reorderList(ListNode *head) {if (!head || !head->next) return;stack<ListNode*> st;ListNode* cur = head;while (cur) {st.push(cur);cur = cur->next;}cur = head;int size = st.size();while (st.size() > size/2) {ListNode* temp1 = cur->next;cur->next = st.top();st.pop();cur->next->next = temp1;cur = temp1;}cur->next->next = nullptr;return;}

环形链表/约瑟夫问题

https://www.nowcoder.com/share/jump/9321389651694182994267
用队列模拟更容易理解:

    int ysf(int n, int m) {// write code herequeue<int> people;for (int i = 1; i <= n; i++) {people.push(i);}while (people.size() > 1) {for (int i = 1; i < m; i++) {//只要在这个循环里,说明都没报到m,都需要从队列头到队列尾people.push(people.front());people.pop();}people.pop();//出循环了,表示当前就是报m的时候了,此时直接pop,不需要再加入队列}return people.front();}

用双向链表list

class Solution {
public:/*** 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可** * @param n int整型 * @param m int整型 * @return int整型*/int ysf(int n, int m) {// write code herelist<int> people;for (int i = 1; i <= n; i++) {people.push_back(i);}auto it = people.begin();//迭代器it记录当前报数的人while (people.size() > 1) {for (int i = 1; i < m; i++) {it++;if (it == people.end()) {//当当前这个it是队尾的后一个元素时,重置为开头,实现环it = people.begin();}}//这个循环结束后,it在报到m的位置it = people.erase(it);//删除报到m的人,迭代器更新if (it == people.end()) {//重新判断迭代器的位置,实现环it = people.begin();}}return people.back();}
};

链表插入排序

https://www.nowcoder.com/share/jump/9321389651694184440161

    ListNode* insertionSortList(ListNode* head) {// write code hereif (!head || !head->next) return head;ListNode* dummy = new ListNode(0);ListNode* cur = head;while (cur) {ListNode* pre = dummy;while (pre->next && pre->next->val < cur->val) {pre = pre->next;}ListNode* temp = cur->next;cur->next = pre->next;pre->next = cur;cur= temp;}return dummy->next;}

单链表+1

https://www.nowcoder.com/share/jump/9321389651694185573227

    ListNode* plusOne(ListNode* head) {// write code hereListNode* cur = reverse(head);ListNode* newHead = cur;int index = 1;  // 要加的1while (cur && index) {int sum = cur->val + index;cur->val = sum % 10;index = sum / 10;if (!cur->next && index) {cur->next = new ListNode(index);break;}cur = cur->next;}return reverse(newHead);}

旋转链表

    ListNode* rotateLinkedList(ListNode* head, int k) {// write code hereif (!head || k == 0) return head;ListNode* fast = head;ListNode* slow = head;int len = 1;// 计算链表长度并移动fastfor (; fast->next; len++) {fast = fast->next;}// 计算实际需要移动的步数k %= len;if (k == 0) return head;// 移动slow到正确的位置for (int i = 0; i < len - k - 1; i++) {slow = slow->next;}// 进行旋转操作ListNode* newHead = slow->next;slow->next = nullptr;fast->next = head;return newHead;}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/75440.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

OpenCV(二十二):均值滤波、方框滤波和高斯滤波

目录 1.均值滤波 2.方框滤波 3.高斯滤波 1.均值滤波 OpenCV中的均值滤波&#xff08;Mean Filter&#xff09;是一种简单的滤波技术&#xff0c;用于平滑图像并减少噪声。它的原理非常简单&#xff1a;对于每个像素&#xff0c;将其与其周围邻域内像素的平均值作为新的像素值…

数据库基础——数据库、数据表和SQL语句

数据库、数据表和SQL语句 数据库、数据表和SQL语句是什么&#xff1f;数据库安装数据库登录及退出创建、查看数据库及修改名字查看及修改数据库编码删除数据库使用或查看当前正在使用的数据库创建、查看数据表及修改名字查看及修改数据表编码查看及修改数据表结构增加约束删除约…

模型训练:优化人工智能和机器学习,完善DevOps工具的使用

作者&#xff1a;JFrog大中华区总经理董任远 据说法餐的秘诀在于黄油、黄油、更多的黄油。同样&#xff0c;对于DevOps而言&#xff0c;成功的三大秘诀是自动化、自动化、更高程度的自动化&#xff0c;而这一切归根结底都在于构建能够更快速地不断发布新版软件的流程。 尽管人…

【Python程序设计】 工厂模式【07/8】

一、说明 我们探索数据工程中使用的设计模式 - 软件设计中常见问题的可重用解决方案。 以下文章是有关 Python 数据工程系列文章的一部分&#xff0c;旨在帮助数据工程师、数据科学家、数据分析师、机器学习工程师或其他刚接触 Python 的人掌握基础知识。 迄今为止&#xff0c;…

Postman接口测试流程

一、工具安装 ● 安装Postman有中文版和英文版&#xff0c;可以选择自己喜欢的版本即可。安装时重新选择一下安装路径&#xff08;也可以默认路径&#xff09;&#xff0c;一直下一步安装完成即可。&#xff08;本文档采用英文版本&#xff09;安装文件网盘路径链接&#xff1…

【分享】golang windows 运行报错 undefined: syscall.SIGUSR1

在跟着煎鱼大佬学习 Golang-gin的时候&#xff0c;"在优雅的重启服务篇" ,为了gin服务的热更新&#xff0c;采用了 endlessfresh的方案&#xff0c;安装endless后无法在windows本地调试,然后报错。 (优雅的重启服务-地鼠文档优雅的重启服务-我不怎么喜欢左写写&#…

蓝桥杯官网练习题(旋转)

题目描述 图片旋转是对图片最简单的处理方式之一&#xff0c;在本题中&#xff0c;你需要对图片顺时针旋转 90 度。 我们用一个 nm 的二维数组来表示一个图片&#xff0c;例如下面给出一个 34 的 图片的例子&#xff1a; 1 3 5 7 9 8 7 6 3 5 9 7 这个图片顺时针旋转 90 …

蓝桥杯官网填空题(振兴中华)

题目描述 本题为填空题&#xff0c;只需要算出结果后&#xff0c;在代码中使用输出语句将所填结果输出即可。 小明参加了学校的趣味运动会&#xff0c;其中的一个项目是&#xff1a;跳格子。 地上画着一些格子&#xff0c;每个格子里写一个字&#xff0c;如下所示&#xff1…

dnmp运行时404报错

dnmp运行时404报错 问题截图&#xff1a; dnmp简介 M1芯片&#xff08;Arm CPU&#xff09; 环境中搭建PHPNGINXMYSQL的利器&#xff0c;docker容器管理当前使用的软件&#xff0c;可以简单安装软件和扩展。 localhost.conf 原始文件如下&#xff1a; server {listen 8…

springmvc 获取项目中的所有请求路径

springboot/springmvc 获取项目中的所有请求路径 1. 编写业务代码 Autowiredprivate WebApplicationContext applicationContext;GetMapping("/getAllURL")public RestfulResult getAllURL() {// 获取springmvc处理器映射器组件对象 RequestMappingHandlerMapping无…

海康NVR(Network Video Recorder)启用SSH过程摸索

文章目录 海康NVR具备的特点启用SSH模式优劣比较启用SSH模式的优势启用SSH模式的坏处 Hik NVR启用SSH功能1&#xff0c;Web登录NVR2&#xff0c;SSH登录NVR SSH shell模式特点SSH shell模式指令作用1&#xff0c;简要帮助“help”可以列出常用的shell指令部分可用shell指令输出…

Android Handler 机制解析

1、前言 在 Android 开发中&#xff0c;Handler 的机制和运行原理这方面的知识可以说是每个人都需要熟悉的。这不仅是因为 Handler 是 Android 应用的基石之一&#xff0c;也因为 Handler 整体设计上也是十分优秀的。接下来我就梳理总结一下常见的 Handler 相关知识点。 2、基…

修复 ChatGPT 发生错误的问题

目录 ChatGPT 发生错误&#xff1f;请参阅如何修复连接错误&#xff01; 修复 ChatGPT 发生错误的问题 基本故障排除技巧 检查 ChatGPT 的服务器状态 检查 API 限制 检查输入格式 清除浏览数据 香港DSE是什么&#xff1f; 台湾指考是什么&#xff1f; 王湘浩 生平 …

【漏洞复现】EnjoySCM存在文件上传漏洞

漏洞描述 EnjoySCM是一款适应于零售企业的供应链管理软件,主要为零售企业的供应商提供服务。EnjoySCM的目的是通过信息技术,实现供应商和零售企业的快速、高效、准确的信息沟通、管理信息交流。。 该系统存在任意文件上传漏洞,攻击者通过漏洞可以获取服务器的敏感信息。 …

【C#项目实战】控制台游戏勇士斗恶龙(1)——游戏初始设置以及开始界面

君兮_的个人主页 即使走的再远&#xff0c;也勿忘启程时的初心 C/C 游戏开发 Hello,米娜桑们&#xff0c;这里是君兮_&#xff0c;最近开始正式的步入学习游戏开发的正轨&#xff0c;想要通过写博客的方式来分享自己学到的知识和经验&#xff0c;这就是开设本专栏的目的。希望…

植物大战僵尸各种僵尸攻略

前言 此文章为“植物大战僵尸”专栏中的009刊&#xff08;2023年9月第八刊&#xff09;&#xff0c;欢迎订阅。版权所有。 注意&#xff1a; 1.本博客适用于pvz无名版&#xff1b; 2.pvz指植物大战僵尸&#xff08;Plants VS Zonbies)&#xff1b; 3.本文以耗费低做标准&am…

老太太阿姨收割机秀才被封

除了他自己和平台官方&#xff0c;恐怕没有人知道详细数字&#xff0c;不过坊间流传着一句话&#xff0c;叫“秀才和一笑倾城一场直播&#xff0c;就可以榨光一个省的老人低保 可见吸金是有多么恐怖 一笑倾城是秀才的“姊妹篇”&#xff0c;秀才专供老太太&#xff0c;一笑倾城…

uni-app:自带的消息提示被遮挡的解决办法(自定义消息提示框)

效果&#xff1a; 代码&#xff1a; 1、在最外层或者根组件的模板中添加一个容器元素&#xff0c;用于显示提示消息。例如&#xff1a; <div class"toast-container" v-if"toastMessage"><div class"toast-content">{{ toastMessa…

EMERSON A6500-CC 机架接口模块 AMS参数

EMERSON A6500-CC 机架接口模块 AMS参数 ModBus和机架接口模块设计用于工厂的高可靠性 最关键的旋转机械。它从所有AMS A6500 ATG模块读取参数 并通过ModBus TCP/IP和/或ModBus RTU&#xff08;串行&#xff09;输出这些参数。 此外&#xff0c;OPC UA可用于向第三方系统传输数…

华为Mate 60和iPhone 15选哪个?

最近也有很多朋友问我这个问题来着&#xff0c;首先两款手机定位都是高端机&#xff0c;性能和体验各有千秋&#xff0c;各自有自己的铁杆粉。 但是让人意想不到的是华为mate60近日在海外越来越受欢迎和追捧&#xff0c;甚至是引起了不少人的抢购&#xff0c;外观设计和…