2023-07-31 LeetCode每日一题(重排链表)

chatgpt/2023/9/27 7:26:10

2023-07-31每日一题

一、题目编号

143. 重排链表

二、题目链接

点击跳转到题目位置

三、题目描述

给定一个单链表 L 的头节点 head ,单链表 L 表示为:

	L0 → L1 → … → Ln - 1 → Ln

请将其重新排列后变为:

	L0 → Ln → L1 → Ln - 1 → L2 → Ln - 2 → …

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

示例 1:
在这里插入图片描述
示例 2:
在这里插入图片描述
提示:

  • 链表的长度范围为 [1, 5 * 104]
  • 1 <= node.val <= 1000

四、解题代码

/*** 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 {ListNode* middleNode(ListNode* head){ListNode* dummyHead = new ListNode(0);dummyHead->next = head;ListNode* fast = dummyHead;ListNode* slow = dummyHead;while(fast->next != nullptr){fast = fast->next;slow = slow->next;if(fast->next != nullptr){fast = fast->next;}}return slow;}void reverseListNode(ListNode* head1, ListNode* tail){ListNode* p = head1->next;tail = p;while(p != nullptr){ListNode* q = p;p = p->next;if(q == tail){tail->next = nullptr;continue;}head1->next = q;q->next = tail;tail = q;}}
public:void reorderList(ListNode* head) {ListNode* mid = middleNode(head);reverseListNode(mid, mid->next);ListNode* head1 = head;ListNode* head2 = mid->next;while(head1 != nullptr && head2 != nullptr){if(head1 == mid){head1->next = nullptr;}ListNode*p = head1;head1 = head1->next;if(head1 == mid){head1->next = nullptr;}p->next = head2;ListNode*q = head2;head2 = head2->next;q->next = head1;}}
};

五、解题思路

(1) 使用分治的思路来解决问题。

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

如若内容造成侵权/违法违规/事实不符,请联系郑州代理记账网进行投诉反馈,一经查实,立即删除!

相关文章

Netty 执行了多次channelReadComplete()却没有执行ChannelRead()

[TOC](Netty 执行了多次channelReadComplete()) Survive by day and develop by night. talk for import biz , show your perfect code,full busy&#xff0c;skip hardness,make a better result,wait for change,challenge Survive. happy for hardess to solve denpendies.…

809协议

809协议 目录概述需求&#xff1a; 设计思路实现思路分析1.809协议数据流——链路管理类 参考资料和推荐阅读 Survive by day and develop by night. talk for import biz , show your perfect code,full busy&#xff0c;skip hardness,make a better result,wait for change,…

MySQL和Oracle区别

由于SQL Server不常用&#xff0c;所以这里只针对MySQL数据库和Oracle数据库的区别 (1) 对事务的提交 MySQL默认是自动提交&#xff0c;而Oracle默认不自动提交&#xff0c;需要用户手动提交&#xff0c;需要在写commit;指令或者点击commit按钮 (2) 分页查询 MySQL是直接在SQL…

Rust- match匹配

The match expression in Rust is a powerful tool that allows you to handle multiple outcomes of a pattern match operation, very similar to a switch statement in other languages but way more powerful. In its simplest form, it can be used to match values:

VSCode搭建GCC环境

1. 下载 https://www.mingw-w64.org/downloads/ https://github.com/niXman/mingw-builds-binaries/releases 2.安装 x86_64-12.2.0-release-win32-seh-rt_v10-rev1.7z解压到D盘 我的电脑–属性–系统属性–环境变量–系统变量–path D:\MinGW-w64\x86_64-12.2.0-release…

【【萌新的stm32学习-2】】

萌新的stm32学习-2 STM32 启动模式 可以通过BOOT0和BOOT1 引脚设置启动模式 BOOT1 BOOT0 X 0 启动模式 主闪存存储器 0 1 系统存储器 1 1 选择 内置SRAM&#xff08;少&#xff09; 我们使用串口给 STM32 下载程序&#xff0c;但是串口下载并不能仿真调试代码&#xff0c;只…

react实现markdown

参考&#xff1a;https://blog.csdn.net/Jack_lzx/article/details/118495763 参考&#xff1a;https://blog.csdn.net/m0_48474585/article/details/119742984 0. 示例 用react实现markdown编辑器 1.基本布局及样式 <><div classNametf_editor_header>头部&…

前端面试的性能优化部分(3)每篇10题

21.如何优化移动端网页的性能&#xff1f; 优化移动端网页的性能是提升用户体验、降低用户流失的关键。以下是一些优化移动端网页性能的常见方法&#xff1a; 压缩和合并资源&#xff1a; 压缩 CSS、JavaScript 和图片等静态资源&#xff0c;减少文件大小&#xff0c;同时合并…
推荐文章