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

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;
}

 

相关文章:

  • 解答UnityShader学习过程中的一些疑惑(持续更新中)
  • 在 Spring Boot 中实现异常处理的全面指南
  • Callable Future 实现多线程按照顺序上传文件
  • 知识付费平台推荐及对比介绍
  • 更新日期自动填充
  • ESG跨境电商怎么样?esg跨境电商有哪些功用?
  • 【动手学大模型开发】使用 LLM API:ChatGPT
  • 使用Curl进行本地MinIO的操作
  • 30天通过软考高项-第六天
  • MTKAndroid12-13-开机应用自启功能实现
  • Vue 对话框出现时,为什么滚动鼠标还是能滚动底层元素
  • Spring系列四:AOP切面编程第三部分
  • 软件工程(一):黑盒测试与白盒测试
  • 如何在WordPress网站中设置双重验证,提升安全性
  • 打火机检测数据集VOC+YOLO格式925张1类别
  • 案例篇:如何用tcpdump和Wireshark识别潜在威胁
  • Finish技术生态计划: FinishRpc
  • 无线采发仪多通道 在结构健康与地质灾害监测中的应用 VS-Box振弦、温度及多信号采发仪
  • 【Vue.js】Vue3父子组件数据通信案例(基础案例)
  • Vue组件开发进阶:从通信原理到DOM异步更新实战
  • 卸任兰大校长后,严纯华院士重返北大作报告
  • 今年一季度全国社会物流总额达91万亿元,工业品比重超八成
  • 葡萄牙总理:未来几小时内将全面恢复供电
  • 日月谭天丨赖清德强推“脱中入北”是把台湾带向死路
  • 杭州打造商业航天全产业链,请看《浪尖周报》第22期
  • 印度媒体称印巴在克什米尔再次交火