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的更多相关文章

  1. algorithm & bitwise operation & the best leetcode solutions

    algorithm & bitwise operation & the best leetcode solutions leetcode 136 single-number the b ...

  2. 【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 ...

  3. [Leetcode Week6]Reorder List

    Reorder List 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/reorder-list/description/ Description G ...

  4. [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 ...

  5. 【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 ...

  6. 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 ...

  7. 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 ...

  8. [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 ...

  9. 【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 ...

随机推荐

  1. C++基础知识---static const初始化成员变量

    为了限制常数的范围class中.你必须要做出成为class成员:而要确保这是丝毫不亚于有一个恒定的实体.你必须要做出成为static员: Class Gameplayer { Private: Sta ...

  2. sleep和wait的区别

    sleep指线程被调用时,占着CPU不工作,形象地说明为“占着CPU睡觉”,此时,系统的CPU部分资源被占用,其他线程无法进入,会增加时间限制.wait指线程处于进入等待状态,形象地说明为“等待使用C ...

  3. HTML学习笔记之中的一个(input文件选择框的封装)

    方式一:直接透明隐藏 .file_button_container,.file_button_container input {background: transparent url(./img/BT ...

  4. Storm-0.9.2-incubating源代码编译打包

    近期遇到一些同学询问Storm-0.9.2-incubating源代码编译打包的问题,现将编译步骤说明例如以下: 1.凝视掉project各pom文件里关于maven插件(maven-gpg-plug ...

  5. pygame在安装过程中无法找到videodev.h错误

    首先参考<ubuntu 安装 pygame 非常好玩的东西>.在运行sudo python setup.py时.出现 linux/videodev.h:No such file or di ...

  6. Nginx对某个文件夹或整个站点进行登录认证的方法

    比方要对 站点文件夹下的 test 文件夹 进行加密认证 首先须要在opt 的主文件夹中 /opt/ 创建一个新文件 htpasswd 此文件的书写格式是 username:password 每行一个 ...

  7. IIS架构与HTTP请求处理流程

    IIS架构与HTTP请求处理流程 Windows操作系统中的IIS负责提供互联网服务,一台运行了IIS的计算机可以看成是一台Web服务器. Windows XP SP2 中IIS主版本号为5,Wind ...

  8. 安装ArcGIS License 10.1 许可管理器 破解版 服务启动又失败的解决办法

    安装破解文件的提示执行 替换许可管理器Bin下面的service.txt  文件,之后会发现,许可管理器启动不了(有时候又可以,挺郁闷), 经过多次的试验,我找到了一种折中解决的方法,供大家参考 解决 ...

  9. [ACM] POJ 3259 Wormholes (bellman-ford最短路径,推断是否存在负权回路)

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 29971   Accepted: 10844 Descr ...

  10. 修ecshop品牌筛选以LOGO图片形式显示

    如何实现商品列表页属性筛选区品牌筛选以LOGO形式展示,最模板总结ecshop/'>ecshop教程入下: 1.修改 category.php 文件,将(大概215行) $sql = " ...