题目要求:

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.

思路:

初看到这个题目,只是提出了空间复杂度的要求,并没有提出时间复杂度要求,因此我开始的思路比较简单。

首先固定一个位置指针,指示每次要插入的位置;

查找链表的最后一个结点;

将链表的最后一个结点插入到指示位置;

位置指针下移一步。

结束条件是:进行了链表长度的1/2次

该种思路运行结果正确,但是超时了。。。。

好坑啊,你也没要提示我时间复杂度啊

思路2:

考虑到时间复杂度的问题,又采取了另外一种思路。

首先使用快慢指针,找到链表的中心;

将链表的后半部分进行逆序;

逆序完成后,将后半部分依次插入到指定位置。

该种方法时间复杂度为O(n),空间复杂度为O(1)

代码:

public static void reorderList(ListNode head){
if(head == null || head.next == null || head.next.next == null){
return;
}else{
ListNode ptr = head, curptr = null, fast = null, slow = null;
int len = 0; //记录链表长度
while(ptr != null){
len++;
ptr = ptr.next;
}

//slow指针走到链表中心
slow = head;
fast = head;
while(fast != null && fast.next != null){
slow = slow.next;
fast = fast.next.next;
}

//查找链表中心的前一个元素
ListNode ptr1 = head, ptr2 = slow;
while(ptr1.next != slow){
ptr1 = ptr1.next;
}
//后半部分链表逆序
if(len%2 == 0){
//偶数个元素
fast = slow.next;
for(int i = 0; i < len/2-1; i++){
ptr = fast.next;
fast.next = slow;
slow = fast;
fast = ptr;
}
ptr1.next = null;
ptr2.next = null;

//偶数个元素的链表逆序完成后依次插入
curptr = head;
ListNode ptr3 = null;
for(int i=0; i<len/2; i++){
ptr = curptr.next;
curptr.next = slow;
ptr3 = slow.next;
slow.next = ptr;
curptr = ptr;
slow = ptr3;
}
}else{
//奇数个元素
slow = slow.next;
ptr1 = slow;
fast = slow.next;
for(int i= 0; i < len/2-1;i++){
ptr = fast.next;
fast.next = slow;
slow = fast;
fast = ptr;
}
ptr2.next = null;
ptr1.next = null;

//奇数个元素逆序后依次插入元素
curptr = head;
ListNode ptr3 = null;
for(int i = 0; i < len/2; i++){
ptr = curptr.next;
curptr.next = slow;
ptr3 = slow.next;
slow.next = ptr;
curptr = ptr;
slow = ptr3;
}
}

}
}

leetcode143- Reorder List问题的更多相关文章

  1. leetcode143. Reorder List

    用快慢双指针,可以使慢指针到达中间的时候快指针到达最后一个元素(奇数),或者倒数第二个元素(偶数).慢指针后面的元素是后半个链表,把后半个链表进行reverse,然后再插在原来的链表中就可以了 /** ...

  2. Leetcode143. Reorder List重排链表

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

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

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

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

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

  7. 13. Reorder List

    Reorder List Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… Y ...

  8. String reorder

    本问题出自:微软2014实习生及秋令营技术类职位在线测试 (Microsoft Online Test for Core Technical Positions) Description For th ...

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

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

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

随机推荐

  1. Irrlicht引擎I 配置

    游戏是一个比较大的系统,包含了图形引擎.网络.AI.声音.UI等模块,模块的开发可能会分别进行或者采用开源项目,Irrlicht引擎基本包含了这些模块,不过在使用中也会陆续加入其它的模块.以前开发的程 ...

  2. design the relations

    Computer Science An Overview _J. Glenn Brookshear _11th Edition A pivotal step in designing a relati ...

  3. 挑战编程PC/UVa Stern-Brocot代数系统

    /* Stern-Brocot代数系统 Stern-Brocot树是一种生成所有非负的最简分数m/n的美妙方式. 其基本方式是从(0/1, 1/0)这两个分数开始, 根据需要反复执行如下操作: 在相邻 ...

  4. Machine Learning in Action -- 树回归

    前面介绍线性回归,但实际中,用线性回归去拟合整个数据集是不太现实的,现实中的数据往往不是全局线性的 当然前面也介绍了局部加权线性回归,这种方法有些局限 这里介绍另外一种思路,树回归 基本思路,用决策树 ...

  5. WCF中自定义消息编码器:压缩编码器的使用

    通过抓包知道WCF在提交.返回数据的时候大多使用XML进行数据交互,如果返回DataTable那么这些数据将变得很大,通过查询找到一个对数据压缩的方法: http://msdn.microsoft.c ...

  6. HelloWorld之jetty运行

    jetty是一个轻便的嵌入式servlet容器.其启动运行非常简单.eclipse下运行jetty容器有如下几步, 一.建一个普通的java工程 二.把jetty需要的包导入工程分别是jetty-6. ...

  7. Qt的学习资料比起其它C/C++的GUI组件来说已经算很全的了

    Qt的学习资料比起其它C/C++的GUI组件来说已经算很全的了.Google的话能解决很多问题,如果没搜到资料的话,如果不是问题太过具体或者奇葩,那就是搜索方法的问题.中文教程中,Qt学习之路系列很不 ...

  8. 帝国CMS备忘

    一. 2级导航: 类似下图这种导航: 实现方式如: 1. 定义一个标签模板,记住ID,具体内容如: 页面模板内容: <li><a href=”[!—bclassurl—]”>[ ...

  9. (leetcode)Implement Stack using Queues

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  10. xdebug和xhprof

    在安装时出现不是:1% 不是有效的win32 应用程序原因可能是是下载了64位的.dll扩展与当前的php不兼容