https://oj.leetcode.com/problems/reorder-list/

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

解题思路:

这道题可以化解为三个子问题。第一,将一个链表分解为两半,也就是取出中间一个节点。第二,对后半链表进行倒置。第三,将前半链表和后半倒置后的链表归并。

第一个问题,前面已经处理过很多次了。可以首先next出整个链表的大小,然后在从头往后N/2个节点。也可以用快慢指针,快指针往后两个,慢指针才往后一个。这样,当快指针到达链表最后,慢指针指向的就是整个链表的中间。这里要注意的是,快指针走的方法,要判断两次。还有,这里取得的midNode实际上是前半链表的最后一个节点,而不是后半链表的第一个节点。

第二个问题,在 Reverse Nodes in k-Group 问题中也做过。

第三个问题,每次用两个引用保存下两个节点就可以。

对于链表的操作还是比较综合的,考虑好几个操作,不是太难,但是需要很细致。掌握的原则还是,链表的特点是可以往后取对象,不能往前。所以不能太快。

/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public void reorderList(ListNode head) {
if(head == null || head.next == null){
return;
} //实际上后半段链表的头节点为midNode的下一个节点
ListNode midNode = getMidNode(head);
ListNode secondHead = midNode.next;
midNode.next = null;
secondHead = reverseList(secondHead); //firstList: head-midNode-null
//secondList: secondHead-end-null
mergeList(head, secondHead);
} public ListNode getMidNode(ListNode head){
if(head == null || head.next == null){
return head;
}
ListNode fastNode = head;
ListNode slowNode = head;
while(fastNode.next != null){
fastNode = fastNode.next;
if(fastNode.next != null){
fastNode = fastNode.next;
slowNode = slowNode.next;
}
}
return slowNode;
} public ListNode reverseList(ListNode head){
if(head == null || head.next == null){
return head;
} ListNode preNode = head;
ListNode currentNode = head.next;
ListNode nextNode = head.next.next;
preNode.next = null; while(currentNode != null){
currentNode.next = preNode;
preNode = currentNode;
currentNode = nextNode;
//这个要特别注意,极可能nextNode已经null了
if(nextNode != null){
nextNode = nextNode.next;
}
}
return preNode;
} //两个链表归并,1-2,3-4归并为1-3-2-4
public void mergeList(ListNode head, ListNode secondHead){
while(head != null && secondHead != null){
ListNode headNext = head.next;
head.next = secondHead;
ListNode secondHeadNext = secondHead.next;
secondHead.next = headNext;
head = headNext;
secondHead = secondHeadNext;
}
}
}

上面的方法的时间复杂度为O(n)。之前还写了一个方法,思路是,每次都找到当前链表的最后一个节点,插入到前面的某个位置。例如,1-2-3-4-5-6,然后1-6-2-3-4-5,然后1-6-2-5-3-4。所以每个循环内,都要需要另外O(n)的时间去寻找尾节点,时间复杂度就为O(n^2)了,会time limit exceeded。还是提供下代码吧。

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

update 2015/05/20:

二刷。这题包含寻找链表的中间节点、reverse list、merge list三个链表中比较重要的问题。可以说是leetcode里,链表题目中最为综合的一道,比较重要。一遍AC。

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public void reorderList(ListNode head) {
if(head == null || head.next == null) {
return;
}
ListNode preMid = getPreMidNode(head);
// 中间节点是preMid.next
ListNode head2 = reverse(preMid.next);
preMid.next = null;
// 将head和head2开头的两个listmerge
while(head != null && head2 != null) {
ListNode next = head2.next;
head2.next = head.next;
head.next = head2;
head = head2.next;
head2 = next;
}
} // 实际上取得的是中间节点的前一个节点,为了方便将其next置为null
public ListNode getPreMidNode(ListNode head) {
ListNode fast = head;
ListNode slow = head;
while(fast.next != null) {
fast = fast.next;
if(fast.next != null) {
fast = fast.next;
slow = slow.next;
}
}
return slow;
} // 将以head开头的list倒置
public ListNode reverse(ListNode head) {
if(head == null || head.next == null) {
return head;
}
ListNode pre = head;
ListNode cur = head.next;
pre.next = null;
while(cur != null) {
ListNode next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
return pre;
}
}

Reorder List 最典型的linkedlist题目的更多相关文章

  1. Leetcode: Reorder List && Summary: Reverse a LinkedList

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

  2. [LeetCode] 动态规划入门题目

    最近接触了动态规划这个厉害的方法,还在慢慢地试着去了解这种思想,因此就在LeetCode上面找了几道比较简单的题目练了练手. 首先,动态规划是什么呢?很多人认为把它称作一种"算法" ...

  3. JDK1.8源码(六)——java.util.LinkedList 类

    上一篇博客我们介绍了List集合的一种典型实现 ArrayList,我们知道 ArrayList 是由数组构成的,本篇博客我们介绍 List 集合的另一种典型实现 LinkedList,这是一个有链表 ...

  4. [转]POJ3624 Charm Bracelet(典型01背包问题)

    来源:https://www.cnblogs.com/jinglecjy/p/5674796.html 题目链接:http://bailian.openjudge.cn/practice/4131/ ...

  5. [SinGuLaRiTy] 动态规划题目复习

    [SinGuLaRiTy-1026] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. [UVA 1025] A Spy in the Metr ...

  6. [SinGuLaRiTy] 分治题目复习

    [SInGuLaRiTy-1025] Copyrights (c) SinGuLaRiTy 2017. All Rights Reserved. [POJ 1905] 棍的膨胀 (Expanding ...

  7. LeetCode 11月第2周题目汇总

    开源地址:点击该链接 前言 最近比较忙,这周几乎没有刷题,只刷了6道题~ 题目汇总 0387_first_unique_character_in_a_string类似的题目比较多了,字符串中找出特别的 ...

  8. leetcode: 数组

    1. longest-consecutive-sequence Given an unsorted array of integers, find the length of the longest ...

  9. leetcode字节跳动专题(持续更新)

    挑战字符串 无重复字符的最长子串 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最 ...

随机推荐

  1. YOLO训练Pedestrain

    Pedestrain dl  使用darknet训练: 1. Inria 创建 yolo-inria.cfg 从cfg/yolo-voc.2.0.cfg拷贝一份,修改batch=64, subdivi ...

  2. Shiny学习实践01

    Shiny是什么东东? 官方描述: Shiny is an R package that makes it easy to build interactive web apps straight fr ...

  3. CXF-JAX-WS开发(一)入门案例

    一.Web Service 1.定义 W3C定义,Web服务(Web service)应当是一个软件系统,用以支持网络间不同机器的互动操作. 2.作用 多系统间数据通信 二.CXF是什么? CXF是目 ...

  4. Cell期刊论文:为什么计算机人脸识别注定超越人类?(祖母论与还原论之争)

    终于找到ML日报的微信链接,抄之...................................... 请拜访原文链接:[祖母论与还原论之争]为什么计算机人脸识别注定超越人类?评价:       ...

  5. PHP安装环境搭建

    一. 安装PHP运行服务器 xampps-x64 二.安装PHP程序编辑软件(Zend Studo需要破解) 安装后打开,再关闭 把com.zend.php.core_10.6.0.v20140128 ...

  6. InnoDB undo log物理结构的初始化

    水平有限,如果有误请指出.一直以来未对Innodb 的undo进行好好的学习,最近刚好有点时间准备学习一下,通过阿里内核月报和自己看代码的综合总结一下.本文环境: 代码版本 percona 5.7.2 ...

  7. PAT_A1076#Forwards on Weibo

    Source: PAT A1076 Forwards on Weibo (30 分) Description: Weibo is known as the Chinese version of Twi ...

  8. Pytorch基础(5)——批数据训练

    一.知识点: 相关包:torch.utils.data import torch import torch.utils.data as Data 包装数据类:TensorDataset [包装数据和目 ...

  9. linux基础随记

    1.cd 切换路径 cd ~vbird 进入这个用户的主目录cd ~ 进入root这个目录下cd .. 进入root上层目录cd - 进入root这个目录下cd /var/spool/mail 直接访 ...

  10. 【2】数据采集 - urllib模块

    python2环境下关于urllib2的使用可以学习这篇文章.本文主要针对python3环境下使用urllib模块实现简单程序爬虫. 链接:https://www.jianshu.com/p/3183 ...