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

Java实现:

1、使用快慢指针来找到链表的中点,并将链表从中点处断开,形成两个独立的链表;
2、将第二个链翻转;
3、将第二个链表的元素间隔地插入第一个链表中。

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public void reorderList(ListNode head) {
if(head==null||head.next==null){
return;
}
ListNode slow=head;
ListNode fast=head;
while(fast!=null&&fast.next!=null){
slow=slow.next;
fast=fast.next.next;
}
fast=slow.next;
slow.next=null;
ListNode pre=null;
ListNode next=null;
while(fast!=null){
next=fast.next;
fast.next=pre;
pre=fast;
fast=next;
}
slow=head;
fast=pre;
ListNode post1=null;
ListNode post2=null;
while(slow!=null&&fast!=null){
post1=slow.next;
post2=fast.next;
slow.next=fast;
fast.next=post1;
slow=post1;
fast=post2;
}
}
}

参考:https://www.cnblogs.com/grandyang/p/4254860.html

143 Reorder List 重排链表的更多相关文章

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

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

  3. Leetcode143. Reorder List重排链表

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

  4. Java实现 LeetCode 143 重排链表

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

  5. 【Warrior刷题笔记】143.重排链表 【线性化 || 双指针+翻转链表+链表合并】详细注释

    题目一 力扣143.重排链表 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/reorder-list/ 1.描述 给定一个单链表L的头节点he ...

  6. Leetcode 143.重排链表

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

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

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

  8. 【LeetCode】143. Reorder List 解题报告(Python)

    [LeetCode]143. Reorder List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  9. 143. Reorder List - LeetCode

    Question 143. Reorder List Solution 题目大意:给一个链表,将这个列表分成前后两部分,后半部分反转,再将这两分链表的节点交替连接成一个新的链表 思路 :先将链表分成前 ...

随机推荐

  1. Struts2的配置文件——web.xml

    任何MVC框架都需要与Web应用整合,这就不得不借助于web.xml文件,只有配置在web.xml文件中Servlet才会被应用加载. 通常,所有的MVC框架都需要Web应用加载一个核心控制器,对于S ...

  2. passive aggressive(pa)和average perceptron(ap)

    passive aggressive(pa)和average perceptron(ap)

  3. iOS 获取WIFI SSID及MAC地址

    NSString *ssid = @"Not Found"; NSString *macIp = @"Not Found"; CFArrayRef myArra ...

  4. Linux时间子系统之(一):时间的基本概念【转】

    本文转载自:http://www.wowotech.net/timer_subsystem/time_concept.html 本文使用Q & A的方式来和大家以前探讨一下时间的基本概念 一. ...

  5. android广播接收器

    Android程序创建广播接收器继承BroadcastReceiver Android广播接收器需要在AndroidManifest.xml文件中声明: <recevie android:nam ...

  6. Apriori算法实例

    Apriori算法与实例 R. Agrawal 和 R. Srikant于1994年在文献[2]中提出了Apriori算法,该算法的描述如下: 下面是一个具体的例子,最开始数据库里有4条交易,{A.C ...

  7. Struts2的各种标签库

    1 在JSP中使用taglib编译指令导入标签库 <%@ taglib prefix="s" uri="/struts-tags" %> ----- ...

  8. BZOJ_1713_[Usaco2007 China]The Bovine Accordion and Banjo Orchestra 音乐会_斜率优化

    BZOJ_1713_[Usaco2007 China]The Bovine Accordion and Banjo Orchestra 音乐会_斜率优化 Description Input 第1行输入 ...

  9. IOCP编程小结(中)

    上一篇主要谈了一些基本理念,本篇将谈谈我个人总结的一些IOCP编程技巧. 网络游戏前端服务器的需求和设计 首先介绍一下这个服务器的技术背景.在分布式网络游戏服务器中,前端连接服务器是一种很常见的设计. ...

  10. 洛谷P1999三国游戏

    题目:https://www.luogu.org/problemnew/show/P1199 仔细想想,两方都拿不到每个武将的配对中最大的: 本来想的是如果有武将a,b,对应最大ma,mb,次大ca, ...