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. UITableViewStyleGrouped 模式下 headview 多出一块高度问题

    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.tableView ...

  2. Hibernate中的一级缓存、二级缓存和懒加载

    1.为什么使用缓存 hibernate使用缓存减少对数据库的访问次数,从而提升hibernate的执行效率.hibernate中有两种类型的缓存:一级缓存和二级缓存. 2.一级缓存 Hibenate中 ...

  3. NIO基础

    通道和缓冲区 概述 通道 和 缓冲区 是 NIO 中的核心对象,几乎在每一个 I/O 操作中都要使用它们. 通道是对原 I/O 包中的流的模拟.到任何目的地(或来自任何地方)的所有数据都必须通过一个 ...

  4. linux命令每日一练习-ls

    ls列出目录下所有文件 ls -l列出具体信息. drwxr-xr-x  9 root root      4096 2011-11-01 tomcat6.0.32 第一个d表示是目录,如果是-表示普 ...

  5. Android 时间戳的转换

    在Android应用中,经常会碰到后台的时间是时间戳而现实的需要今天什么时候,昨天什么时候,就像微博的时间显示一样.现在我上一个把时间戳转换的代码: public static String getT ...

  6. 使用OCI向Oracle插入Geometry数据

    使用C/C++操作Oracle数据库,使用OCI可谓是最强大,当然也是最难的方式.Oracle是一个功能复杂而强大的数据库,它可以很好的支持空间数据(Oracle spatial).如何使用OCI向O ...

  7. 【转】./configure && make && make install详解

    在Linux中利用源码包安装软件最重要的就是要仔细阅读安装包当中的README  INSTALL两个说明文件,这两个文件会清楚的告诉你如何可以正确的完成这个软件的安装!          我们都知道源 ...

  8. python练习——水仙花数

    题目: 请判断一个数是不是水仙花数.其中水仙花数定义各个位数立方和等于它本身的三位数.输入有多组测试数据,每组测试数据以包含一个整数n(100<=n<1000)输入0表示程序输入结束.输出 ...

  9. UNICODE字符集(20140520)

    1多字节字符集,如"IT学吧",sizeof内存长度为7,因为前面2个字母各占用一个字节,后面两个汉字各占用2个字节,结尾的\0占用一个字节.strlen即字符串长度的结果为6. ...

  10. yii2 单页面增删改

    视图层 <style>#tab tr td{    height:40px;    width:100px;}</style><form action="ind ...