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→- 你不能只是单纯的改变节点内部的值,而是需要实际的进行节 ...
随机推荐
- EnhanceFunc__增强函数集
想将经常用到的功能函数写在一起,花时间精心维护,然后以后就用起来就舒服很多了 目前就写了进程调试权限,远程线程注入,远程线程释放这三个函数.还有很多功能,以后慢慢加 // last code by g ...
- 基于Netty的RPC架构学习笔记(八):protocol buff学习使用
文章目录 简介 准备 protobuf配置文件 生成java代码 举个
- JVM内核-原理、诊断与优化学习笔记(十):Class文件结构
文章目录 语言无关性 文件结构 魔数 版本 常量池 CONSTANT_Utf8 CONSTANT_Integer CONSTANT_String CONSTANT_NameAndType CONSTA ...
- STM32 Keil仿真调试
引用:http://blog.sina.com.cn/s/blog_3c63d2bd0102vt9a.html 问题描述:使用MDK进行软件设计时没有使用ST官方的模板而是手动建立的工程,使用ST官方 ...
- Activiti学习笔记9 — UserTask共有任务的使用
1.创建流程引擎对象 private ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); 2.发布一个流程 ...
- YARN设计思路
- jpa 踩坑 SQLGrammarException
SQLGrammarException could not execute query cause by not found column id ,, id指的是,返回的结果没有Id 封装结果集出错 ...
- error while loading shared libraries: lib*.so: cannot open shared object file: No such file or directory
动态库的搜索路径搜索的先后顺序是: 1.编译目标代码时指定的动态库搜索路径; 2.环境变量LD_LIBRARY_PATH指定的动态库搜索路径: 比如export LD_LIBRARY_PATH=/us ...
- INSTALL_FAILED_TEST_ONLY oppor11p 安装时出现的问题
刚开始出现这个问题 我很懵逼, 因为我之前一直运行都好好的 !!我在网上查了好多资料 试了好几种办法 我先说下我成功的办法 方法1: Android Studio 3.0会在debu ...
- 一个切图仔的 JS 笔记
1,常用数据操作 Math.round(mum,2);num.toFixed(2);两位数 Math.floor(); 返回不大于的最大整数 Math.ceil(); 则是不小于他的最小整数 Math ...