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. Hadoop Spark 集群简便安装总结

    本人实际安装经验,目的是为以后高速安装.仅供自己參考. 一.Hadoop 1.操作系统一如既往:①setup关掉防火墙.②vi /etc/sysconfig/selinux,改SELINUX=disa ...

  2. ACM-简单题之Least Common Multiple——hdu1019

    ***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...

  3. MySQL 触发器结构及三个案例demo

    --你必须拥有相当大的权限才能创建触发器(CREATE TRIGGER),如果你已经是Root用户,那么就足够了.这跟SQL的标准有所不同. CREATE TRIGGER语法 CREATE TRIGG ...

  4. hadoop日志分析

    一.项目要求 本文讨论的日志处理方法中的日志,仅指Web日志.事实上并没有精确的定义,可能包含但不限于各种前端Webserver--apache.lighttpd.nginx.tomcat等产生的用户 ...

  5. Linux智能小开关rfkill

    Linux智能小开关rfkill Rfkill,当中rf是Radio frequency(射频).主要作用是一个专门管理开关的子系统,举例说明Android手机的通知栏能够方便地开关Airplane/ ...

  6. Linux------创建和终止进程

    创建进程: Linux创建两个步骤的新处理:fork()和exec().其中fork创建当前进程的能力(父进程)副本,那个孩子.父子进程只有PID不同.在这之后,该系统具有两个进程,运行相同的操作.父 ...

  7. SSH是什么?Linux如何修改SSH端口号?

    通过SSH连接可以远程管理Linux等设备,默认linuxssh端口是22端口,如何修改SSH默认端口,如何增加SSH端口呢?,下面小编给大家演示一下   工具/原料 Xshell   putty 等 ...

  8. SessionFactory的创建和Session的获得

    1.当我们调用 Configuration config=new Configuration().configure(); 时候Hibernate会自己主动在当前的CLASSPATH中搜寻hibern ...

  9. 【ECSHOP插件】商品颜色尺寸仿淘宝选择功能免费发布

    先放效果图,如此实用的功能是不是迫不及待的要添加到自己的网店中了呢   牵涉到的修改文件(default模板为例) /themes/default/style.css /themes/default/ ...

  10. php判断变量是否存在

    isset— 检测变量是否设置, isset() 只能用于变量,因为传递任何其它参数都将造成解析错误.若想检测常量是否已设置,可使用 defined() 函数. 如果已经使用 unset() 释放了一 ...