题目要求:

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. ArcGIS Server 缓存服务切图范围

    win10 + Server 10.4 +  ArcMap 10.4  ArcGIS Server 缓存服务分为创建服务后手动建立缓存和创建服务时同时自动建立缓存两种. 10.2帮助文档:http:/ ...

  2. WSDL Style和use的组合方式说明

    原文地址:http://www.ibm.com/developerworks/cn/webservices/ws-whichwsdl/#listing9 use属性可以为literal,encoded ...

  3. 费用性支出预分摊form方式和web方式区别

    预分摊 1 form方式 费用性支出先通过生产资产行请求 生成一个资产行,如果该资产行分摊到两个资产上则分割成两个资产行,既所谓的针对资产行进行分摊. 2 web方式 费用性支出直接分摊到资产上形成资 ...

  4. Dlib is a modern C++ toolkit(非常全面的类库)

    http://dlib.net/ http://download.csdn.net/detail/lajuedan2508/9726225 http://download.csdn.net/detai ...

  5. php--validate表单验证

    validate表单验证扩展规则 添加自定义检验(验证class) 获取html加入 class <input id="D_NUMBER" name="D_NUMB ...

  6. 转:VS2010调试NUnit测试项目 (Running or debugging NUnit tests from Visual Studio without any extensions)

    If you write unit tests and use NUnit test framework this may be helpful. I decided to write this si ...

  7. Eclipse中直接双击执行bat时路径问题

    之前bat中使用的是 cd %cd% 这样在文件夹中直接运行bat是没问题的 但在eclipse中运行, 取得的路径就是eclipse.exe的所在路径 而如果需要获得bat文件的实际所在路径 应该使 ...

  8. VMware中CentOS设置静态IP

    因为之前搭建的MongoDB分片没有采用副本集,最近现网压力较大,所以准备研究一下,于是在自己电脑的虚拟机中搭建环境,但是发现之前VMware设置的是DHCP,所以每次重新resume后虚拟机中IP都 ...

  9. c#上传文件(二)使用文件流保存文件

    1.html代码: <asp:FileUpload runat="server" ID="UpLoadFile"/> <asp:Button ...

  10. linux压缩解压

    zip压缩文件 zip -r filename.zip filesdir zip -r filename.zip file1 file2 file3 /usr/work/school //将file1 ...