Given a singly linked list LL0→L1→…→Ln-1→Ln,
reorder it to: L0→LnL1→Ln-1→L2→Ln-2→…

You must do this in-place without altering the nodes' values.

For example,
Given {1,2,3,4}, reorder it to {1,4,2,3}.

按照题意改变链表结构。

1、使用list记录链表。

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public void reorderList(ListNode head) { List<ListNode> list = new ArrayList<ListNode>();
ListNode node = head;
while( node != null ){
list.add(node);
node = node.next;
}
int len = list.size();
if( len < 3)
return ; node = head;
node.next = list.get(len-1);
node = node.next;
for( int i = 1;i<len/2;i++){ node.next = list.get(i);
node.next.next = list.get(len-1-i);
node = node.next.next;
}
if( len%2 != 0){
node.next = list.get(len/2);
node = node.next;
}
node.next = null; return ;
}
}

2、使用双向队列。

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public void reorderList(ListNode head) { Deque<ListNode> deque = new ArrayDeque<ListNode>(); ListNode node = head; while( node != null ){
deque.add( node );
node = node.next;
}
if( deque.size() < 3)
return ;
node = deque.poll();
node.next = deque.pollLast();
node = node.next;
while( !deque.isEmpty() ){
node.next = deque.poll();
node.next.next = deque.pollLast();
node = node.next.next;
}
if( node != null )
node.next = null;
return ;
}
}

3、不使用额外空间,找到中间点,然后将后半部分链表反转,然后将两个链表合并即可。

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public void reorderList(ListNode head) { if( head == null || head.next == null || head.next.next == null )
return ;
ListNode slow = head;
ListNode fast = head.next;
while( fast!= null && fast.next != null){
fast = fast.next.next;
slow = slow.next;
} ListNode node2 = slow;
fast = slow.next; while( fast != null ){ ListNode node = fast.next;
fast.next = slow;
slow = fast;
fast = node; }
node2.next = null;
node2 = slow;
ListNode node1 = head; while( node1 != null ){
ListNode node = node1.next;
ListNode nn = node2.next;
node1.next = node2;
node2.next = node;
node1 = node;
node2 = nn;
} return ;
}
}

leetcode 143. Reorder List ----- java的更多相关文章

  1. leetcode 143. Reorder List 、86. Partition List

    143. Reorder List https://www.cnblogs.com/grandyang/p/4254860.html 先将list的前半段和后半段分开,然后后半段进行逆序,然后再连接 ...

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

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

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

  5. [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 mod ...

  6. Leetcode#143 Reorder List

    原题地址 先把链表分割成前后两半,然后交叉融合 实践证明,凡是链表相关的题目,都应该当成工程类题目做,局部变量.功能函数什么的随便整,代码长了没关系,关键是清楚,不容易出错. 代码: ListNode ...

  7. 【LeetCode】143. Reorder List 解题报告(Python)

    [LeetCode]143. Reorder List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  8. 143. Reorder List - LeetCode

    Question 143. Reorder List Solution 题目大意:给一个链表,将这个列表分成前后两部分,后半部分反转,再将这两分链表的节点交替连接成一个新的链表 思路 :先将链表分成前 ...

  9. LeetCode第[18]题(Java):4Sum 标签:Array

    题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...

随机推荐

  1. bzoj 2301 莫比乌斯反演

    对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数. 这里题目意思很明显 对于要求的f[n] = sig ...

  2. Ad Muncher 宣布免费

    Windows平台广告过滤软件Ad Muncher宣布免费 详见:http://www.admuncher.com/free 下载:http://www.admuncher.com/static/fi ...

  3. LOGISTIC REGRESSION

    In logistic regression we learn a family of functions

  4. C++ primer的第三章的主要内容

    第三章主要介绍了C++中标准库类型.主要讲到string和vector类型.在string类型中,能够很方便的操作字符串,应该要注意的地方就是它的字符串中元素的位置的类型是:size_type类型的数 ...

  5. 2013年8月份第3周51Aspx源码发布详情

    BaiduMusic Cache源码  2013-8-23 [VS2012]功能介绍:可以读取并保存百度音乐(Win8应用程序商店版本)的缓存.可以检测电脑是否安装了百度音乐,支持缓存音乐的导出功能. ...

  6. 内部类中class声明地方不同,效果不一样

    1.一个声明在类中,一个声明在类的方法中.在类中的方法中声明内部类,其方法中的内部类调用 内部类外中的变量,变量必须final class Outter{ int x1 = 0; public voi ...

  7. PED结构获取进程路径和命令行地址

    1.FS寄存器 2.进入FS寄存器地址,7FFDD000 3.偏移30为PED结构 4.偏移地址10 3C,44偏移:路径地址,命令行地址 // 通过PEB结构去查找所有进程模块 void *PEB ...

  8. Python 温习

    关于Python内置函数的示例 Type "copyright", "credits" or "license()" f重写or more ...

  9. Android AIDL使用详解

    1.什么是aidl:aidl是 Android Interface definition language的缩写,一看就明白,它是一种android内部进程通信接口的描述语言,通过它我们可以定义进程间 ...

  10. Swift编程规范

    文档编号: 应用开发Swift编码规范 (版本v1.0.0)       成文信息 主题词: Swift开发编码规范 作  者: 周少停 文档类别: 开发规范 审  核: 批  准: 文档性质: 初稿 ...