题目:

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

思路:

先用快慢指针找到链表的中点,然后翻转链表后半部分,再和前半部分组合。需要注意的是把链表分成两半时,前半段的尾节点要置为NULL,翻转链表时也要把尾节点置为NULL。

注意:后半部分可能比前半部分长,所以最后要判断一下是否将后半部分全部加入链表。

  1. /**
  2. * Definition for singly-linked list.
  3. * function ListNode(val) {
  4. * this.val = val;
  5. * this.next = null;
  6. * }
  7. */
  8. /**
  9. * @param {ListNode} head
  10. * @return {void} Do not return anything, modify head in-place instead.
  11. */
  12. var reorderList = function(head) {
  13. if(head==null||head.next==null){
  14. return;
  15. }
  16. var l=head,r=head,lpre=null;
  17. while(r!=null&&r.next!=null){
  18. lpre=l;
  19. l=l.next;
  20. r=r.next.next;
  21. }
  22. lpre.next=null;
  23. r=reverseList(l);
  24. var p=head,temp=null,tempr,tail=null;
  25. while(p!=null){
  26. temp=p.next;
  27. tempr=r;
  28. r=r.next;
  29. p.next=tempr;
  30. tail=p.next;
  31. tempr.next=temp;
  32. p=temp;
  33. }
  34.  
  35. if(r!=null){
  36. tail.next=r;
  37. }
  38.  
  39. };
  40.  
  41. var reverseList = function(head) {
  42. if(head==null||head.next==null){
  43. return head;
  44. }
  45. var temp=null,pre=null,cur=null;
  46. pre=head;
  47. cur=pre.next;
  48. pre.next=null;
  49.  
  50. while(cur!=null){
  51. temp=cur.next;
  52. cur.next=pre;
  53. pre=cur;
  54. cur=temp;
  55.  
  56. }
  57.  
  58. return pre;
  59. };

【链表】Reorder List的更多相关文章

  1. [Swift]LeetCode143. 重排链表 | 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 ...

  2. LeetCode_算法及数据结构覆盖统计

    [输入]共计151道题的算法&数据结构基础数据 (见附录A) [输出-算法]其中有算法记录的共计 97道 ,统计后 结果如下  top3(递归,动态规划,回溯) 递归 动态规划 回溯 BFS ...

  3. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  4. LeetCode总结【转】

    转自:http://blog.csdn.net/lanxu_yy/article/details/17848219 版权声明:本文为博主原创文章,未经博主允许不得转载. 最近完成了www.leetco ...

  5. leetcode解题文件夹

    点击打开链接点击打开链接点击打开链接參考文献:http://blog.csdn.net/lanxu_yy/article/details/17848219 只是本文准备用超链接的方式连接到对应解答页面 ...

  6. [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 ...

  7. 62. 链表重排[Reorder List]

    [本文链接] http://www.cnblogs.com/hellogiser/p/reorder-list.html [题目] Given a singly linked list L: L0→L ...

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

  9. 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 thi ...

  10. 给乱序的链表排序 · Sort List, 链表重排reorder list LoLn...

    链表排序 · Sort List [抄题]: [思维问题]: [一句话思路]: [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入): [画图]: quick ...

随机推荐

  1. 前端程序员经常忽视的一个 JavaScript 面试题

    题目 function Foo() { getName = function () { alert (1); }; return this; } Foo.getName = function () { ...

  2. oss上传文件夹-cloud2-泽优软件

    泽优软件云存储上传控件(cloud2)支持上传整个文件夹,并在云空间中保留文件夹的层级结构,同时在数据库中也写入层级结构信息.文件与文件夹层级结构关系通过id,pid字段关联. 本地文件夹结构 文件 ...

  3. 配置 cxf-ws spring bean 文件

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  4. 树和二叉树在java中

    树代表一种非线性的数据结构,如果一组数组节点之间存在复杂的一对多关联时,程序就可以考虑使用树来保存这组数据了. 线性表.栈和队列都是线性的数据结构,这种数据结构之内的元素只存在一个对一个的关系.存储, ...

  5. Chinese Seals

    Emperors used seals to proclaim their decrees to their people, officials used seals to exercise thei ...

  6. 集合(二)LinkedList

    上一篇中讲解了ArrayList,本篇文章讲解一下LinkedList的实现. LinkedList是基于链表实现的,所以先讲解一下什么是链表.链表原先是C/C++的概念,是一种线性的存储结构,意思是 ...

  7. [Linux-vi] The simple set of vi command

    Source : https://www.cs.colostate.edu/helpdocs/vi.html What is vi? The default editor that comes wit ...

  8. lua的table的删除操作

    直接使用table的remove方法 , , } table.remove(tab) --默认删除最后一个元素,第二个参数可以指定删除位置 删除后,后面的元素会往前移动.有点像C++的std::vec ...

  9. ECG心电图数据2

    1.如何下载获取MIT-BIH的数据从下面这个官方链接页面可以下载到所有48组MIT-BIH心电数据: http://www.physionet.org/physiobank/database/mit ...

  10. Sublime Text 3 格式化HTML CSS JS 代码

    一,首先通过ctrl+shift+p 要等一会就会出现插件安装界面 二,在插件安装输入框,输入:HTML-CSS-JS Prettify  并安装该插件 三,如果没有装nodejs, 下载nodejs ...