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→- 你不能只是单纯的改变节点内部的值,而是需要实际的进行节 ...
随机推荐
- 数据结构C++版-栈
一.概念 二.应用实例 1.进制转换 #include <stdlib.h> #include <iostream> #include <string> #incl ...
- linux网络速率监控
#!/bin/bash #作者:fafu_li #时间: #监控网卡传输速率 source /etc/profile #加载系统环境变量 source $HOME/.bash_profile #加载用 ...
- AsyncAwait
using System; using System.Diagnostics; using System.Threading; using System.Threading.Tasks; namesp ...
- Windows下安装配置PLSQL
说明:1.PLSQL Developer是远程连接Oracle数据库的一个可视化工具,并且其不是一个独立的软件,是需要依赖Oracle客户端运行的.2.本安装教程是基于本机没有安装Oracle数据库的 ...
- scrapy的使用--Rcrapy-Redis
Scrapy-Redis分布式爬虫组件 Scrapy是一个框架,他本身是不支持分布式的.如果我们想要做分布式的爬虫.就需要借助一个组件叫做Scrapy-Redis.这个组件正式利用了Redis可以分布 ...
- 利用DNSQuery 进行DNS查询
#include <WinSock2.h> #include <WinDNS.h> #pragma comment (lib, "Dnsapi.lib") ...
- redis config
网络上抄袭过来的 然后顺序自己做点笔记 http://chembo.iteye.com/blog/2054021 这里有各个key 详细的描述 # redis 配置文件示例 # 当你需要为某个配置项指 ...
- Algo: Basic
1. 二维数组的查找 2. 替换空格 3. 从尾到头打印链表 4. 重建二叉树 5. 用两个栈实现队列 6. 旋转数组的最小数字 7. 斐波那契数列 8. 跳台阶 9. 变态跳台阶 10. 矩阵覆盖 ...
- [JZOJ6258] 【省选模拟8.9】轰炸
题目 题目大意 给你一棵树和树上的许多条从后代到祖先的链,选择每条链需要一定代价,问覆盖整棵树的所有点的最小代价是多少. \(n,m\leq 100000\) 正解 (由于时间过于久远,所以直接说正解 ...
- mySql搜索引擎
转载自 深入浅出mysql数据库 MySQL5.5以后默认使用InnoDB存储引擎,其中InnoDB和BDB提供事务安全表,其它存储引擎都是非事务安全表. 若要修改默认引擎,可以修改配置文件中的def ...