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

You may not modify the values in the list's nodes, only nodes itself may be changed.

思路:

1.  find mid

2.  cut list

3.  reverse slow part

4.  merge two parts

代码:

 class Solution {
public void reorderList(ListNode head) {
// corner case
if(head == null || head.next == null) return;
// init
ListNode fast = head;
ListNode slow = head;
ListNode prev = null;
// find mid
while(fast != null && fast.next != null){
prev = slow;
slow = slow.next;
fast = fast.next.next;
}
//cut list
prev.next = null;
//recurse slow part
slow = reverse(slow);
fast = head;
// merge two parts
while(fast.next!=null){
ListNode temp = fast.next;
fast.next = slow;
slow = slow.next;
fast.next.next = temp;
fast = temp;
}
fast.next = slow;
} private ListNode reverse(ListNode head){
ListNode cur = head;
ListNode pre = null;
while(cur != null){
ListNode temp = cur.next;
cur.next = pre;
pre=cur;
cur=temp;
}
return pre;
}
}
												

[leetcode]143. Reorder List重排链表的更多相关文章

  1. 143 Reorder List 重排链表

    给定一个单链表L:L0→L1→…→Ln-1→Ln,重新排列后为: L0→Ln→L1→Ln-1→L2→Ln-2→…必须在不改变节点的值的情况下进行原地操作.例如,给定链表 {1,2,3,4},按要求重排 ...

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

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

  3. [Leetcode] Reorder list 重排链表

    Given a singly linked list L: L 0→L 1→…→L n-1→L n,reorder it to: L 0→L n →L 1→L n-1→L 2→L n-2→… You ...

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

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

  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

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

  8. Leetcode143. Reorder List重排链表

    给定一个单链表 L:L0→L1→-→Ln-1→Ln , 将其重新排列后变为: L0→Ln→L1→Ln-1→L2→Ln-2→- 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换. 示例 1: ...

  9. Java实现 LeetCode 143 重排链表

    143. 重排链表 给定一个单链表 L:L0→L1→-→Ln-1→Ln , 将其重新排列后变为: L0→Ln→L1→Ln-1→L2→Ln-2→- 你不能只是单纯的改变节点内部的值,而是需要实际的进行节 ...

随机推荐

  1. web项目除了业务还需要关注的点

    1:安全性,不允许访问外网,访问外网通过反向代理的方式. 2:安全性,和外网交互的时候,需要CA证书,基于SSL协议的证书 3:日志,生产上通常会关闭某些日志,所以,允许出现的日志就显得至关重要了. ...

  2. setTimeout 方法带参数传递

    setTimeout(callback, after, arg1, arg2); 其中,callback即function(){},after为时间参数,指多久后执行callback,单位为毫秒,30 ...

  3. WinRAR 代码执行漏洞复现

    影响版本: WinRAR < 5.70 Beta 1 Bandizip    < = 6.2.0.0 好压(2345压缩)    < = 5.9.8.10907 360压缩    & ...

  4. HTML5拖动

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  5. RAD 10 C++Builder的bug

    C++Builder的bug 修改一行代码,F9会报错.要clear工程重新完整编译才可以. 新建空白工程是好的. restart computer ok!!! 2)fdquery like this ...

  6. Spring MVC 运行流程图

  7. UI5-文档-4.20-Aggregation Binding

    现在我们已经为我们的应用建立了一个良好的结构,是时候添加更多的功能了.通过添加一些JSON格式的发票数据,我们开始探索数据绑定的更多特性,这些发票数据显示在面板下面的列表中. Preview A li ...

  8. mtr 命令详解

    一般在windows 来判断网络连通性用ping 和tracert,ping的话可以来判断丢包率,tracert可以用来跟踪路由,在Linux中有一个更好的网络连通性判断工具,它可以结合ping ns ...

  9. Struts和Hibernate使用总结

    1   struts.xml重定向时报错    action cannot be found in the namespace/     http://blog.csdn.net/greetturin ...

  10. sql server 2000能否得到一个表的最后更新日期?

    如果是SQL 2005 或 2008.运行下面的代码.就可以看到从上次启动SQL 服务以来,某个表的使用情况,包括select/update/delete/insert. SELECT * FROM ...