LeetCode Solutions : 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 this in-place without altering the nodes' values.
For example,
Given {1,2,3,4}, reorder itto {1,4,2,3}.
Considering the following steps:
 * 1. split such list into two list, first and second, according to slow and fast point
 * 2. reverse the second list
 * 3. insert the second list into the first list
coding solution:
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*
*/
public class Solution {
public void reorderList(ListNode head) {
if(head!=null&&head.next!=null){
ListNode low=head;//1. split such list into two list, first and second, according to slow and fast point
ListNode fast=head;
while(fast.next!=null&&fast.next.next!=null){
low=low.next;
fast=fast.next.next;
}
ListNode first=head;
ListNode second=low.next;
low.next=null; second=reverse(second);//2. reverse the second list
while(second!=null){//3. insert the second list into the first list
ListNode p1=first.next;
ListNode p2=second.next;
first.next=second;
second.next=p1;
first=p1;
second=p2;
} }
}
private ListNode reverse(ListNode head){
if(head==null||head.next==null)
return head;
ListNode pre=head;
ListNode cur=head.next;
while(cur!=null){
ListNode nextNode=cur.next;
cur.next=pre;
pre=cur;
cur=nextNode;
}
head.next=null;
return pre;
}
}
版权声明:本文博主原创文章,博客,未经同意不得转载。
LeetCode Solutions : Reorder List的更多相关文章
- algorithm & bitwise operation & the best leetcode solutions
		algorithm & bitwise operation & the best leetcode solutions leetcode 136 single-number the b ... 
- 【Leetcode】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 ... 
- [Leetcode Week6]Reorder List
		Reorder List 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/reorder-list/description/ Description G ... 
- [LeetCode] 937. Reorder Data in Log Files 日志文件的重新排序
		You have an array of `logs`. Each log is a space delimited string of words. For each log, the first ... 
- 【leetcode】Reorder List (middle)
		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 ----- 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 OJ] Reorder List—Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…
		For example,Given {1,2,3,4}, reorder it to {1,4,2,3}. /** * Definition for singly-linked list. * str ... 
- 【LeetCode】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 ... 
随机推荐
- RabbitMQ消息队列应用
			RabbitMQ消息队列应用 消息通信组件Net分布式系统的核心中间件之一,应用与系统高并发,各个组件之间解耦的依赖的场景.本框架采用消息队列中间件主要应用于两方面:一是解决部分高并发的业务处理:二是 ... 
- phantomjs,selenium,pyv8,pythonwebkit,,,,,,,,,,,,,
			Pyv8,PythonWebKit,Selenium,PhantomJS,Ghost.py 等等.... 快速构建实时抓取集群[searchtb] 定义:http://i.cnblogs.com/Ed ... 
- NYOJ 104 最大子矩阵(二维DP)
			最大和 时间限制:1000 ms | 内存限制:65535 KB 难度:5 描写叙述 给定一个由整数组成二维矩阵(r*c),如今须要找出它的一个子矩阵,使得这个子矩阵内的全部元素之和最大,并把这个 ... 
- SWT的TableVierer的使用一
			1,简单显示,表格的式样见注释中的内容 import org.eclipse.jface.viewers.TableViewer;import org.eclipse.swt.SWT;import o ... 
- 从零开始学Xamarin.Forms(四)  Android 准备步骤(添加第三方Xamarin.Forms.Labs库)
			原文:从零开始学Xamarin.Forms(四) Android 准备步骤(添加第三方Xamarin.Forms.Labs库)  1.安装对应dll Update-Package Xama ... 
- Windows Phone开发(8):关于导航的小技巧
			原文:Windows Phone开发(8):关于导航的小技巧 前文用几个例子对导航做了简单介绍,在一般应用中,使用上一篇文章中说到的方法,其实也够用了,不过,为了能够处理一些特殊的情况,有几个小技巧还 ... 
- erlang集群IP及port管理
			erlang集群是依靠epmd维护的,epmd是erlang集群节点间port映射的守护进程.负责维护集群内的节点连接.提供节点名称到IP地址及port的解析服务. epmd 自己定义port号 ep ... 
- 【ThinkingInC++】61、非成员运算符
			非成员运算符 当操作者的左侧是不同的类时.运算符重载不可能是正确的类中. IostreamOperatorOverloading.cpp /** * 书本:[ThinkingInC++] * 功能:非 ... 
- 垂死或涅槃重生 -- Delphi XE5 我们将宣布感情的回归
			Delphi 在很大程度上是一个被遗忘我的工具. 无论是在使用RapidSql , 我还没有收到Embarcadero 本公司发行参与邀请Delphi XE5该公告将. 可能有人会问,为什么Embar ... 
- listary文件查找程序下载和使用
			资源:PHP开发学习门户网站 地址:http://bbs.phpthinking.com/forum.php?mod=viewthread&tid=173 Listary 是一款有用的国产Wi ... 
