当前位置: 首页 > ai >正文

leetcode 143. 重排链表

题目描述

容易想到的做法是,把后半部分链表反转后的链表和原来的前半部分链表合并,即可得到想要的结果。需要先找链表的中间结点,参考leetcode 876. 链表的中间结点-CSDN博客

反转链表可以用正统的反转链表的方法(leetcode官方即用的此法),也可以用栈来实现。

/*** 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:void reorderList(ListNode* head) {ListNode* slow = head;ListNode* fast = head;while(fast != nullptr && fast->next != nullptr){fast = fast->next->next;slow = slow->next;}stack<ListNode*> node_stack;ListNode* back_half = slow;while(back_half != nullptr){node_stack.push(back_half);back_half = back_half->next;}ListNode* middle = slow;ListNode* ans = new ListNode(0,head);ListNode* tail = nullptr;ListNode* front_half = head;while(front_half != middle && !node_stack.empty()){if(tail == nullptr){tail = front_half;ans->next = tail;}else{tail->next = front_half;tail = tail->next;}front_half = front_half->next;tail->next = node_stack.top();tail = tail->next;node_stack.pop();}while(front_half != middle){if(tail == nullptr){tail = front_half;ans->next = tail;}else{tail->next = front_half;tail = tail->next;}front_half = front_half->next;}while(!node_stack.empty()){if(tail == nullptr){tail = node_stack.top();ans->next = tail;}else{tail->next = node_stack.top();tail = tail->next;}node_stack.pop();}tail->next = nullptr;head = ans->next;delete ans;}
};

如第65行所示,最后一定要把tail->next置为nullptr,不然结果链表中会出现环路。

测试代码:

#include<iostream>
#include<stack>
using namespace std;//  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:void reorderList(ListNode* head) {ListNode* slow = head;ListNode* fast = head;while(fast != nullptr && fast->next != nullptr){fast = fast->next->next;slow = slow->next;}stack<ListNode*> node_stack;ListNode* back_half = slow;while(back_half != nullptr){node_stack.push(back_half);back_half = back_half->next;}cout<<"node_stack.size():"<<node_stack.size()<<endl;ListNode* middle = slow;ListNode* ans = new ListNode(0,head);ListNode* tail = nullptr;ListNode* front_half = head;while(front_half != middle && !node_stack.empty()){if(tail == nullptr){tail = front_half;ans->next = tail;}else{tail->next = front_half;tail = tail->next;}front_half = front_half->next;tail->next = node_stack.top();tail = tail->next;node_stack.pop();}while(front_half != middle){if(tail == nullptr){tail = front_half;ans->next = tail;}else{tail->next = front_half;tail = tail->next;}front_half = front_half->next;}while(!node_stack.empty()){if(tail == nullptr){tail = node_stack.top();ans->next = tail;}else{tail->next = node_stack.top();tail = tail->next;}node_stack.pop();}tail->next = nullptr;head = ans->next;delete ans;}
};int main()
{ListNode* dummy = new ListNode();ListNode* cur = nullptr;for(int i = 1; i<=5;i++){ListNode* pNode = new ListNode(i);if(cur == nullptr){cur = pNode;dummy->next = cur;}else{cur->next = pNode;cur = cur->next;}}cur = dummy->next;while(cur){cout<<cur->val<<",";cur = cur->next;}cout<<endl;Solution so;so.reorderList(dummy->next);cur = dummy->next;while(cur){cout<<cur->val<<",";cur = cur->next;}cout<<endl;return 0;
}

 

http://www.xdnf.cn/news/2639.html

相关文章:

  • js day8
  • Java学习手册: IoC 容器与依赖注入
  • leetcode刷题日记——两数相加
  • 【Redis】基础4:作为分布式锁
  • 搭建speak yarn集群:从零开始的详细指南
  • 关于健身房管理系统前后端软件开发主要功能需求分析
  • 深入理解网络原理:TCP协议详解
  • MCP Servers玩玩WebUI自动化
  • 如何在idea 中写spark程序
  • UARA串口开发基础
  • Dify+DeepSeek实战教程!企业级 AI 文档库本地化部署,数据安全与智能检索我都要
  • OpenResty技术深度解析:原理、应用与生态对比-优雅草卓伊凡
  • 基于 BERT 微调一个意图识别(Intent Classification)模型
  • LinuxAgent开源程序是一款智能运维助手,通过接入 DeepSeek API 实现对 Linux 终端的自然语言控制,帮助用户更高效地进行系统运维工作
  • astrbot_plugin_composting_bucket开源程序是一个用于降低AstrBot的deepseek api调用费用的插件
  • AI大模型:(二)2.4 微调自己的模型
  • 蒋新松:中国机器人之父
  • 解构编程语言的基因密码:论数据类型如何被语言系统定义与重塑
  • 达梦数据库官方迁移工具SQLark:支持Oracle/MySQL/PostgreSQL迁移至达梦数据库!
  • 使用exdp 备份数据库
  • Scratch——第20课 辗转相除法/绳子算法
  • GitLab CVE-2024-12444 安全漏洞解决方案
  • 劳动节ppt免费下载,劳动节ppt模板,劳动节课件
  • 配置电子邮件服务
  • LabVIEW开发之困境中逼出成长力
  • MCP之二_服务器与客户端实现
  • 抱佛脚之学SSMAOP
  • 【AI News | 20250428】每日AI进展
  • 国内比较好用的代理IP测评
  • C++——哈希表