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 解释: 因为无重复字符的最 ...
随机推荐
- MTK刷机工具Flash_Tool部分4032错误解决办法
MTK刷机工具Flash_Tool部分4032错误解决办法 先说明一点,这个办法不是万能的,我测试解决了以下两种情况下的4032: 1.本来正常的开发板,因为一次刷机失败后就一直变4032了 2.新开 ...
- SAP computer之program counter
Program counter The program is stored in memory with the first instruction at binary address 0000, t ...
- YTBro Video 2016-07-30 Sat
明日天气:多云转阴,32~24度 运动向 健身 20中极致疯狂的俯卧撑,新手请勿尝试 跑酷 大神放大招,招招见血,不信你看 赛事 2016年世界室内田径锦标赛 男子4×400接力决赛 台球 RP集中爆 ...
- (2)dotnet开源电商系统-brnshop VS nopCommerce(dotnet两套电商来PK--第二篇:代码从哪开始-BrnMall3.0Beta)
看大牛们的源码,对于水平一般的人,还是略微有点难度的.我从我自身读码的亲身体验,写下杂散片语,希望能和大家一同进步,也为了日后记忆上的备查. 先看的是brnMall的源码结构,从哪看起呢? 首先推荐看 ...
- SpringBoot入门系列(转)
SpringBoot入门系列:第一篇 Hello World http://blog.csdn.net/lxhjh/article/details/51711148
- 怎样在PDF文件中查找某个特定的词?
不得不说中国的修饰词太多了例如:“滚”可以这样说,请你以一种圆润的方式离开:上次小编在路上听到某男子打电话,好像是给女孩子,那口才,是真的牛,夸人不带重复的.要不是我男孩子,我都想以身相许了.人们常常 ...
- 实现Modbus ASCII多主站应用
1.更新设计关于原来的协议栈在Modbus ASCII主站应用时所存在的局限性与Modbus RTU也是一样的,所以我们不分析它的不足,只讨论更新设计.我们将主站及其所访问的从站定义为通用的对象,而当 ...
- Django 模型层(标签、过滤器、模板的继承与导入)
过滤器/自定义过滤器 模板语法中的过滤器类似于python中的内置方法,在我们把数据从后端通过rander传入到前端html文件中之后,在前端我们可以通过模板语法,对传入的数据再进行以通骚操作. 首先 ...
- jmeter的性能监控框架搭建记录(Influxdb+Grafana+Jmeter)
查看笔记 http://note.youdao.com/noteshare?id=c700365713abb98bd3d10e6f45393af9&sub=6F4E14FF3F9D4167AE ...
- 【编程工具】Vim编辑器的使用
1.Vim简介 Vim最初起源于古老的贝尔实验室,由"Bram Moolenaar等人"开发,是一个功能强大的文本编辑器,被推崇为类Vi编辑器中最好的一个. Vim是一个类 ...