Leetcode143. Reorder List重排链表
给定一个单链表 L:L0→L1→…→Ln-1→Ln ,
将其重新排列后变为: L0→Ln→L1→Ln-1→L2→Ln-2→…
你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。
示例 1:
给定链表 1->2->3->4, 重新排列为 1->4->2->3.
示例 2:
给定链表 1->2->3->4->5, 重新排列为 1->5->2->4->3.
思路:
根据题的意思,可以理解成,将链表分成两半,将后一半倒着插入第一半当中,
那么就好办了。
将后一半反转,再合并。
class Solution {
public:
void reorderList(ListNode* head)
{
if (head == NULL)
return;
int len = 0;
ListNode *head1 = head;
while (head1)
{
len++;
head1 = head1->next;
}
if (len <= 2)
return;
int half = len / 2;
int cnt = len - half;
head1 = head;
ListNode *last = NULL;
while (cnt)
{
cnt--;
last = head1;
head1 = head1->next;
}
last->next = NULL;
ListNode *head2 = NULL;
last = head1;
head1 = head1->next;
///
last->next = NULL;
while (head1 !=NULL)
{
ListNode *node = head1 ->next;
head1->next = last;
last = head1;
head1 = node;
}
head2 = last;
while (head != NULL && head2 != NULL)
{
//cout << "2" << endl;
ListNode *next1 = head->next;
ListNode *next2 = head2 -> next;
head->next = head2;
///
head2->next = next1;
head = next1;
head2 = next2;
}
}
};
Leetcode143. Reorder List重排链表的更多相关文章
- [Leetcode] Reorder list 重排链表
Given a singly linked list L: L 0→L 1→…→L n-1→L n,reorder it to: L 0→L n →L 1→L n-1→L 2→L n-2→… You ...
- 143 Reorder List 重排链表
给定一个单链表L:L0→L1→…→Ln-1→Ln,重新排列后为: L0→Ln→L1→Ln-1→L2→Ln-2→…必须在不改变节点的值的情况下进行原地操作.例如,给定链表 {1,2,3,4},按要求重排 ...
- [leetcode]143. Reorder List重排链表
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You may not mod ...
- L2-022 重排链表 (25 分)
L2-022 重排链表 (25 分) 给定一个单链表 L1→L2→⋯→Ln−1→Ln,请编写程序将链表重新排列为 Ln→L1→Ln−1→L2→⋯.例 ...
- L2-022. 重排链表
L2-022. 重排链表 时间限制 500 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一个单链表 L1→L2→...→Ln-1→Ln,请 ...
- pat甲级 团体天梯赛 L2-022. 重排链表
L2-022. 重排链表 时间限制 500 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一个单链表 L1→L2→...→Ln-1→Ln,请 ...
- GPLT天梯赛 L2-022. 重排链表
L2-022. 重排链表 时间限制 500 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一个单链表 L1→L2→...→Ln-1→Ln,请 ...
- Leetcode 143.重排链表
重排链表 给定一个单链表 L:L0→L1→…→Ln-1→Ln ,将其重新排列后变为: L0→Ln→L1→Ln-1→L2→Ln-2→… 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换. 示 ...
- Java实现 LeetCode 143 重排链表
143. 重排链表 给定一个单链表 L:L0→L1→-→Ln-1→Ln , 将其重新排列后变为: L0→Ln→L1→Ln-1→L2→Ln-2→- 你不能只是单纯的改变节点内部的值,而是需要实际的进行节 ...
随机推荐
- cnn知识点汇总
关于卷积神经网络的入门基础知识: https://blog.csdn.net/weixin_42451919/article/details/81381294 卷积神经网络的相关公式推导: htt ...
- VScode中写vue代码 Ctrl+/添加注释失效
1.点击列表的文件——>首选项——>键盘快捷方式,在里面查看 Ctrl+/ 是否有冲突 2.查看右下角的选择语言模式是否是Vue,如下图
- c++-文件分离
实现文件分离 1.头文件中不要使用using namespace,由于c++编译的特性,由于初学还没深入了解,不做具体编译的解释 2.由于没有了命名空间,所以string定义要写成std::strin ...
- 基于Netty的RPC架构学习笔记(六):netty5案例学习
文章目录 netty5服务端入门案例 netty5客户端入门案例 单客户端多连接程序 知识普及 线程池原理图 对象池原理图 对象组原理图 结论 理论结合实际 开干开干 总结 netty5服务端入门案例 ...
- 5、 postman的鉴权
什么是鉴权? 鉴权(authentication)是指验证用户是否拥有访问系统的权利.常用的有两种鉴权方式,一种是session鉴权,一种是jwt鉴权,相对而言,后者居多. 实例: 比如有一个添加角色 ...
- CSS3 RGBA等于RGB加上opacity吗?
在我们前端设计里有两篇教程: CSS3 RGBA colors使用说明 css3中opacity属性学习与实践,他们公别介绍了RGBA,RGB,opacity的用法,这里我们把这三个属性放在一起来考虑 ...
- QT5+Pylon
VS+QT+Pylon:配置一下包含文件和libs,具体参考pylon说明文档. Windows下 Qtcreator+Pylon:建议把include和libs文件夹拷贝至工作目录,然后修改.pr ...
- Linux网络配置 RPM命令 samba服务 Linux目录结构
第一种方法: (1)用root身份登录,运行setup命令进入到 text mode setup utiliy对网络进行配置,这里可以进行ip,子网掩码,默认网关,dns的设置.(2)这时网卡的配置没 ...
- iOS进阶五-RunLoop
简介 RunLoop 运行循环.跑圈 RunLoop的作用主要体现在三方面: 1.保持程序持续运行 2.处理App中的各种事件(比如触摸事件.定时器事件.Selector事件) 3.节省CPU资源,提 ...
- RDD运行原理