Reorder List 最典型的linkedlist题目
https://oj.leetcode.com/problems/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 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题目的更多相关文章
- 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 ...
- [LeetCode] 动态规划入门题目
最近接触了动态规划这个厉害的方法,还在慢慢地试着去了解这种思想,因此就在LeetCode上面找了几道比较简单的题目练了练手. 首先,动态规划是什么呢?很多人认为把它称作一种"算法" ...
- JDK1.8源码(六)——java.util.LinkedList 类
上一篇博客我们介绍了List集合的一种典型实现 ArrayList,我们知道 ArrayList 是由数组构成的,本篇博客我们介绍 List 集合的另一种典型实现 LinkedList,这是一个有链表 ...
- [转]POJ3624 Charm Bracelet(典型01背包问题)
来源:https://www.cnblogs.com/jinglecjy/p/5674796.html 题目链接:http://bailian.openjudge.cn/practice/4131/ ...
- [SinGuLaRiTy] 动态规划题目复习
[SinGuLaRiTy-1026] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. [UVA 1025] A Spy in the Metr ...
- [SinGuLaRiTy] 分治题目复习
[SInGuLaRiTy-1025] Copyrights (c) SinGuLaRiTy 2017. All Rights Reserved. [POJ 1905] 棍的膨胀 (Expanding ...
- LeetCode 11月第2周题目汇总
开源地址:点击该链接 前言 最近比较忙,这周几乎没有刷题,只刷了6道题~ 题目汇总 0387_first_unique_character_in_a_string类似的题目比较多了,字符串中找出特别的 ...
- leetcode: 数组
1. longest-consecutive-sequence Given an unsorted array of integers, find the length of the longest ...
- leetcode字节跳动专题(持续更新)
挑战字符串 无重复字符的最长子串 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最 ...
随机推荐
- 易企CMS主要模板文件介绍
article.tpl 文章内容页模板 catalog.tpl 文章,产品目录页模板 category.tpl 分类页模板 comment.tpl 留言页模板 footer.tpl 页尾模板 head ...
- Android 侦听应用(Package)变化的方法侦听广播
应用的状态变化,包括安装.卸载.更新,是android系统上重要的事件.如何侦听到?有两种方法,一是通过侦听广播,一是实现PackageMonitor. 侦听广播 当Package状态发生变化时, ...
- Android 概览屏幕
文章照搬过来的:原文地址https://developer.android.google.cn/guide/components/recents.html 概览屏幕(也称为最新动态屏幕.最近任务列表或 ...
- Interrupt中断线程
package com.wistron.swpc.ecs.util; public class WrongWayStopThread extends Thread{ public static voi ...
- (转)Arcgis for JS实现台风运动路径与影像范围的显示
http://blog.csdn.net/gisshixisheng/article/details/42025435 首先,看看具体的效果: 初始化状态 绘制中 绘制完成 首先,组织数据.我组织的数 ...
- Java基础学习笔记之:System类;Math类;Arrays类BigInteger,BigDecimal
System类 在API中System类介绍的比较简单,我们给出定义,System中代表程序所在系统,提供了对应的一些系统属性信息,和系统操作.System类不能手动创建对象,因为构造方法被priva ...
- Python—字符串+变量
字符串转义字符格式化内建函数(后延)转义字符用一个特殊的方法表示出一系列不方便写出的内容,比如回车键,换行键,退格键借助反斜杠字符,一旦字符串中出现反斜杠,则反斜杠后面一个火几个字符表示已经不是原来的 ...
- 创建100个目录dir1-dir100一键完成
创建100个目录dir1-dir100将系统中已有文件xxx.txt复制1000份1.txt-1000.txt将文件1-10保存到第一个目录中11-20保存到第三个目录中的形式将所有文件处理完 #!/ ...
- 【转载】java的常见类型转换
//Int型数字转换成String int num1=123456; //方法1 String str1=num1+""; System.out.println(str1); // ...
- Lua操作系统库、流、文件库
Lua操作系统库.流.文件库 1.Lua中所有的操作系统库函数 (1)os.clock() --功能:返回执行该程序cpu花费的时钟秒数 (2)os.time(...) --按参数的内容返回一个时间值 ...