143 Reorder List 重排链表
给定一个单链表L:L0→L1→…→Ln-1→Ln,
重新排列后为: L0→Ln→L1→Ln-1→L2→Ln-2→…
必须在不改变节点的值的情况下进行原地操作。
例如,
给定链表 {1,2,3,4},按要求重排后为 {1,4,2,3}。
详见:https://leetcode.com/problems/reorder-list/description/
Java实现:
1、使用快慢指针来找到链表的中点,并将链表从中点处断开,形成两个独立的链表;
2、将第二个链翻转;
3、将第二个链表的元素间隔地插入第一个链表中。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public void reorderList(ListNode head) {
if(head==null||head.next==null){
return;
}
ListNode slow=head;
ListNode fast=head;
while(fast!=null&&fast.next!=null){
slow=slow.next;
fast=fast.next.next;
}
fast=slow.next;
slow.next=null;
ListNode pre=null;
ListNode next=null;
while(fast!=null){
next=fast.next;
fast.next=pre;
pre=fast;
fast=next;
}
slow=head;
fast=pre;
ListNode post1=null;
ListNode post2=null;
while(slow!=null&&fast!=null){
post1=slow.next;
post2=fast.next;
slow.next=fast;
fast.next=post1;
slow=post1;
fast=post2;
}
}
}
参考:https://www.cnblogs.com/grandyang/p/4254860.html
143 Reorder List 重排链表的更多相关文章
- [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 ...
- [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 ...
- Leetcode143. Reorder List重排链表
给定一个单链表 L:L0→L1→-→Ln-1→Ln , 将其重新排列后变为: L0→Ln→L1→Ln-1→L2→Ln-2→- 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换. 示例 1: ...
- Java实现 LeetCode 143 重排链表
143. 重排链表 给定一个单链表 L:L0→L1→-→Ln-1→Ln , 将其重新排列后变为: L0→Ln→L1→Ln-1→L2→Ln-2→- 你不能只是单纯的改变节点内部的值,而是需要实际的进行节 ...
- 【Warrior刷题笔记】143.重排链表 【线性化 || 双指针+翻转链表+链表合并】详细注释
题目一 力扣143.重排链表 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/reorder-list/ 1.描述 给定一个单链表L的头节点he ...
- Leetcode 143.重排链表
重排链表 给定一个单链表 L:L0→L1→…→Ln-1→Ln ,将其重新排列后变为: L0→Ln→L1→Ln-1→L2→Ln-2→… 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换. 示 ...
- leetcode 143. Reorder List 、86. Partition List
143. Reorder List https://www.cnblogs.com/grandyang/p/4254860.html 先将list的前半段和后半段分开,然后后半段进行逆序,然后再连接 ...
- 【LeetCode】143. Reorder List 解题报告(Python)
[LeetCode]143. Reorder List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 143. Reorder List - LeetCode
Question 143. Reorder List Solution 题目大意:给一个链表,将这个列表分成前后两部分,后半部分反转,再将这两分链表的节点交替连接成一个新的链表 思路 :先将链表分成前 ...
随机推荐
- JasperReport 中文问题解决
1 运行环境 1.1 JasperReport 3.5 JasperReports 是iReport的核心内容.它是一个强有力的开源报表产生工具,可以将内容输出到屏幕上.打印机或生成PDF ...
- NIO原理图
- idea 破解注册方法总结
注册码(无期限) JetbrainsCrack-2.6.2.jar适用于ideaIU-2017.2.之前版本,若版本较新适用 JetbrainsCrack-2.6.3_proc.jar. 其中Jetb ...
- Java IO、BIO、NIO、BIO
一.什么是IO/NIO: IO:即BIO(Blocking IO):面向流的.同步阻塞式IO:(JDK1.4之前唯一的选择) NIO:面向缓冲的.同步非阻塞式IO:三大核心部分:Selector.Ch ...
- 数据结构之 栈与队列--- 走迷宫(深度搜索dfs)
走迷宫 Time Limit: 1000MS Memory limit: 65536K 题目描述 一个由n * m 个格子组成的迷宫,起点是(1, 1), 终点是(n, m),每次可以向上下左右四个方 ...
- HDU4292 Food —— 最大流 + 拆点
题目链接:https://vjudge.net/problem/HDU-4292 Food Time Limit: 2000/1000 MS (Java/Others) Memory Limit ...
- POJ3104 Drying —— 二分
题目链接:http://poj.org/problem?id=3104 Drying Time Limit: 2000MS Memory Limit: 65536K Total Submissio ...
- Silverlight中使用MVVM(1)
Silverlight中使用MVVM(1) Silverlight中使用MVVM(1)--基础 Silverlight中使用MVVM(2)—提高 Silverlight中使用MVVM(3)—进阶 ...
- wukong引擎源码分析之索引——part 2 持久化 直接set(key,docID数组)在kv存储里
前面说过,接收indexerRequest的代码在index_worker.go里: func (engine *Engine) indexerAddDocumentWorker(shard int) ...
- MFC程序中的 _T("") 什么意思?
_T("")就是把引号内的字符串转换为宽字节的Unicode编码 宽字节就是unicode.