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. RabbitMQ消息队列应用

    RabbitMQ消息队列应用 消息通信组件Net分布式系统的核心中间件之一,应用与系统高并发,各个组件之间解耦的依赖的场景.本框架采用消息队列中间件主要应用于两方面:一是解决部分高并发的业务处理:二是 ...

  2. __iomem解析

    以下是在学习电池驱动中遇到的知识点之_iomem A new I/O memory access mechanism Most reasonably current cards for the PCI ...

  3. 公司需求知识自学-Oracle的Package的作用及用法

    Oracle的Package的作用 简化应用设计.提高应用性能.实现信息隐藏.子程序重载. 1.Oracle的Package除 了把存储过程放到一堆儿以外还有没有其他的作用(好处)? 你不觉得把存储过 ...

  4. 【超有用】图解--怎样使用本地的dtd文件映射

    曾经一直非常苦恼,假设电脑上不了网,就比較麻烦了.自己在配置HIbernate的属性的时候.不知道属性名有没有写错.. 如今和大家分享一下,毕竟自己痛苦过了,大家不要和我一样痛苦.

  5. 从零開始学android<ImageSwitcher图片切换组件.二十六.>

    ImageSwitcher组件的主要功能是完毕图片的切换显示,比如用户在进行图片浏览的时候.能够通过button点击一张张的切换显示的图片,并且使用ImageSwitcher组件在每次切换的时候也能够 ...

  6. The OpenGL pipeline

    1. Vertex Data 2. Vertex Shader 3. Tessellation Control Shader 4.Tessellation evaluation Shader 5. G ...

  7. 划分树 静态第k大

    划分树是保存了快速排序的过程的树,可以用来求静态第k小的数 如果,划分树可以看做是线段树,它的左孩子保存了mid-L+1 个 小于等于 a[mid] 的数字,  右孩子保存了 R-mid个大于等于a[ ...

  8. 基于Android 下载文件时,更新UI简单帮助类

    因为在项目开发时.有这种简单需求,问谷歌,网络上也有好多Utils工具类,可是比較冗余.自己就简单的写了一个简单帮助类. /** * 下载文件,更新UI简单帮助类 * * @author jarlen ...

  9. 445port入侵详细解释

    445port入侵具体解释   关于"445port入侵"的内容445port入侵具体解释本站搜索很多其它关于"445port入侵"的内容 445port入侵, ...

  10. OpenCV Haar AdaBoost源代码改进(比EMCV快6倍)

    这几天研究了OpenCV源代码 Haar AdaBoost算法,作了一下改进 1.去掉了全部动态分配内存的操作.对嵌入式系统有一定的速度提升 2.凝视覆盖了大量关键代码 3.降低了代码一半的体积,而且 ...