[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 modify the values in the list's nodes, only nodes itself may be changed.
思路:
1. find mid
2. cut list
3. reverse slow part
4. merge two parts
代码:
 class Solution {
     public void reorderList(ListNode head) {
         // corner case
         if(head == null || head.next == null) return;
         // init
         ListNode fast = head;
         ListNode slow = head;
         ListNode prev = null;
         // find mid
         while(fast != null && fast.next != null){
             prev = slow;
             slow = slow.next;
             fast = fast.next.next;
         }
         //cut list
         prev.next = null;
         //recurse  slow part
         slow = reverse(slow);
         fast = head;
         // merge two parts
         while(fast.next!=null){
             ListNode temp = fast.next;
             fast.next = slow;
             slow = slow.next;
             fast.next.next = temp;
             fast = temp;
         }
         fast.next = slow;
     }
     private ListNode reverse(ListNode head){
         ListNode cur = head;
         ListNode pre = null;
         while(cur != null){
             ListNode temp = cur.next;
             cur.next = pre;
             pre=cur;
             cur=temp;
         }
         return pre;
     }
 }
[leetcode]143. Reorder List重排链表的更多相关文章
- 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 、86. Partition List
		
143. Reorder List https://www.cnblogs.com/grandyang/p/4254860.html 先将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 ...
 - leetcode 143. Reorder List ----- java
		
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 ...
 - Leetcode 143. Reorder List(Medium)
		
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 ...
 - Java for 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 must do th ...
 - Leetcode#143 Reorder List
		
原题地址 先把链表分割成前后两半,然后交叉融合 实践证明,凡是链表相关的题目,都应该当成工程类题目做,局部变量.功能函数什么的随便整,代码长了没关系,关键是清楚,不容易出错. 代码: ListNode ...
 - 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→- 你不能只是单纯的改变节点内部的值,而是需要实际的进行节 ...
 
随机推荐
- 在线学习和在线凸优化(online learning and online convex optimization)—FTRL算法6
 - div+Css绝对定位(absolute)和相对定位(relative)的总结
			
1.没有外Div的情况下 设置绝对定位(absolute)和相对定位(relative)是没有区别的 2.相对定位占位置 而绝对定位不占位置 会漂浮在别的Div之上 3.若父Div没有设置定位,子Di ...
 - Spark数据本地性
			
1.文件系统本地性 第一次运行时数据不在内存中,需要从HDFS上取,任务最好运行在数据所在的节点上: 2.内存本地性 第二次运行,数据已经在内存中,所有任务最好运行在该数据所在内存的节点上: 3.LR ...
 - Linux常用命令的命名来源
			
很多人在学习Linux的时候会疑惑:这么多的Linux名,他们都是怎么被定义的?林纳斯是怎么制定如此花样繁多且数量庞大的命令?今天这篇文章可能会帮你解开疑惑. ## 1. 目录缩写 缩写 | 全称 | ...
 - Flask上下文管理源码分析
			
上下文管理本质(类似于threading.local): 1.每一个线程都会在Local类中创建一条数据: { "唯一标识":{stark:[ctx,]}, "唯一标识& ...
 - Executor框架(四)周期/延时任务ScheduleThreadPoolExecutor
			
ScheduledThreadPoolExecutor 介绍 ScheduledThreadPoolExecutor 是一个可以实现定时任务的 ThreadPoolExecutor(线程池).比 ...
 - python之集合操作
			
1.集合的增操作 add(item):增加至集合中 update(set): 若不存在,更新至集合中 2.集合的删操作(五种) pop(): 随机删除并返回值 remove(item): 删除va ...
 - 【C语言】09条件编译
			
条件编译的概念;通常我们希望程序的其中一部分代码只有在满足一定的情况下才进行编译,否则不参与编译,(只有参与编译的代码最终才能被执行) 这就是条件编译; 基本用法; #if condication01 ...
 - 很详细的curl命令使用大全
			
可以看作命令行浏览器 1.开启gzip请求 curl -I http://www.sina.com.cn/ -H Accept-Encoding:gzip,defalte 2.监控网页的响应时间 cu ...
 - 46. linux下解压.tar.gz文件
			
tar -zxvf jdk-7u55-linux-i586.tar.gz 步骤二:解压jdk-7u55-linux-i586.tar.gz ,执行以下命令: #mkdir /usr/local/jav ...