leetCodeReorderList链表合并
原题
Given a singly linked list L: L0?L1?…?Ln-1?Ln,
reorder it to: L0?Ln?L1?Ln-1?L2?Ln-2?…
You must do this in-place without altering the nodes' values.
For example,
Given {1,2,3,4}, reorder it to {1,4,2,3}.
分析
将链表结点按照第一个 倒数第一个 第二个 倒数第二个 的顺序重新排序
如 1 2 3 4
重新排序后是 1 4 2 3
AC代码
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
//翻转链表
ListNode *reverse(ListNode *head){
if(!head) return;
ListNode * pre = NULL;
while(head){
ListNode * nex = head->next;
head->next = pre;
pre = head;
head = nex;
}
return pre;
}
void reorderList(ListNode* head) {
if(!head || !head->next) return head;
ListNode * fast = head;
ListNode * slow = head;
while(fast->next && fast->next->next){
fast = fast->next->next;
slow = slow->next;
}
fast = slow->next;
//截断
slow->next = NULL;
//翻转后半段
ListNode * p = reverse(fast);
//合并
ListNode * res = head;
ListNode * q = head->next;
while(p && q){
res->next = p;
p = p->next;
res = res->next;
res->next = q;
q = q->next;
res = res->next;
}
if(p) res->next = p;
if(q) res->next = q;
}
};
leetCodeReorderList链表合并的更多相关文章
- 6.5 k个已排好序链表合并为一个排序链表
1 建立链表(带哨兵位的)2 建立最小堆方法3 合并已排好序的k个链表 typedef int DataType; //建立链表 class Link { private: struct Node { ...
- c# 有序链表合并 链表反转
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 【Warrior刷题笔记】143.重排链表 【线性化 || 双指针+翻转链表+链表合并】详细注释
题目一 力扣143.重排链表 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/reorder-list/ 1.描述 给定一个单链表L的头节点he ...
- 牛客网:将两个单调递增的链表合并为一个单调递增的链表-Python实现-两种方法讲解
方法一和方法二的执行效率,可以大致的计算时间复杂度加以对比,方法一优于方法二 1. 方法一: 思路: 1. 新创建一个链表节点头,假设这里就叫 head3: 2. 因为另外两个链表都为单调递增,所 ...
- 【bzoj1486】【[HNOI2009]梦幻布丁】启发式链表合并(详解)
(画师当然是武内崇啦) Description N个布丁摆成一行,进行M次操作.每次将某个颜色的布丁全部变成另一种颜色的,然后再询问当前一共有多少段颜色.例如颜色分别为1,2,2,1的四个布丁一共有3 ...
- 02-线性结构1 两个有序链表序列的合并(15 point(s)) 【链表合并】
02-线性结构1 两个有序链表序列的合并(15 point(s)) 本题要求实现一个函数,将两个链表表示的递增整数序列合并为一个非递减的整数序列. 函数接口定义: List Merge( List L ...
- 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 must do thi ...
- 已有a,b两个链表,每个链表中的结点包括学号、成绩。要求把两个链表合并,按学号升序排列
1.我的思路先将b链表连接在a链表的后面,这个很容易实现,将a链表最后的结点中的p.next改为指向b链表的头结点即可. 再将这个新链表用选择排序即可. 代码如下: #include<stdio ...
- 已有a,b两个链表,每个链表中的结点包括学号,成绩。要求把两个链表合并。按学号升序排列.
#include <stdio.h>#define SIZE sizeof(struct student)struct student{ long num; flo ...
随机推荐
- [skill][vim] 常用技巧与配置
一: 光标行列高亮 可以使用 :help highlight 查看相信帮助可颜色配置. set cursorline set cursorcolumn highlight Cursorline ct ...
- Flink - Scheduler
Job资源分配的过程, 在submitJob中,会生成ExecutionGraph 最终调用到, executionGraph.scheduleForExecution(scheduler) 接着,E ...
- 转:JDK动态代理为什么必须用接口以及与CGLIB的对比
参考链接: JDK动态代理为什么必须用接口以及与CGLIB的对比 文章中提到:试验了JDK动态代理与CGLIB动态代理.从Spring的AOP框架介绍中得知对于使用接口的类,Spring使用JDK动态 ...
- RHEL6.2的安装文档
1 Installing RHEL 6.2 1.1 开始安装 选择“Install or upgrade an existing system”: 1.2 光盘检测 选择“Skip”跳过安装介质的检查 ...
- LeetCode 637 Average of Levels in Binary Tree 解题报告
题目要求 Given a non-empty binary tree, return the average value of the nodes on each level in the form ...
- java 大数据运算 BigInteger BigDecimal
package cn.sasa.demo5; import java.math.BigDecimal; import java.math.BigInteger; public class BigDat ...
- 洛谷P4363 一双木棋chess [九省联考2018] 搜索+hash
正解:记搜+hash 解题报告: 传送门! 因为看到nm范围特别小,,,所以直接考虑爆搜(bushi 先考虑爆搜之后再想优化什么的嘛QwQ 首先对这种都要最优的,就可以直接把答案设为针对某一方,然后题 ...
- JavaScript的cookie和sessionStorage 、localStorage
localStorage.sessionStorage和cookie的区别与用法请见下面的博客: https://segmentfault.com/a/1190000012057010 cookie的 ...
- MongoDB的客户端管理工具--nosqlbooster 连接MongoDB服务器
nosqlbooster的官网地址为https://nosqlbooster.com.大家如果想直接下载,可以登入下载网址https://nosqlbooster.com/downloads. 下载w ...
- shell分析日志常用指令合集
数据分析对于网站运营人员是个非常重要的技能,日志分析是其中的一个.日志分析可以用专门的工具进行分析,也可以用原生的shell脚本执行,下面就随ytkah看看shell分析日志常用指令有哪些吧.(log ...