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

这是一道比较综合的链表操作的题目,要按照题目要求给链表重新连接成要求的结果。其实理清思路也比较简单,分三步完成:(1)将链表切成两半,也就是找到中点,然后截成两条链表;(2)将后面一条链表进行reverse操作,就是反转过来;(3)将两条链表按顺序依次merge起来。

这几个操作都是我们曾经接触过的操作了,第一步找中点就是用runner technique方法,一个两倍速跑,一个一倍速跑,知道快的碰到链表尾部,慢的就正好停在中点了。第二步是比较常见的reverse操作,在Reverse Nodes in k-Group也有用到了,一般就是一个个的翻转过来即可。第三步是一个merge操作,做法类似于Sort List中的merge

接下来看看时间复杂度,第一步扫描链表一遍,是O(n),第二步对半条链表做一次反转,也是O(n),第三部对两条半链表进行合并,也是一遍O(n)。所以总的时间复杂度还是O(n),由于过程中没有用到额外空间,所以空间复杂度O(1)。

 class Solution {
public void reorderList(ListNode head) {
if (head == null) return;
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode p1 = dummy, p2 = dummy;
while (p2 != null && p2.next != null) {
p2 = p2.next.next;
p1 = p1.next;
}
ListNode head2 = p1.next;
p1.next = null;
ListNode head2New = reverse(head2);
merge(head, head2New);
} public ListNode reverse(ListNode head) {
if (head == null) return null;
ListNode p1 = head;
ListNode p2 = head.next;
while (p2 != null) {
ListNode next = p2.next;
p2.next = p1;
p1 = p2;
p2 = next;
}
head.next = null;
return p1;
} public ListNode merge(ListNode l1, ListNode l2) {
if (l2 == null) return l1;
int counter = 0;
ListNode pre = new ListNode(-1);
ListNode cur = pre;
while (l1 != null && l2 != null) {
if (counter % 2 == 0) {
cur.next = l1;
l1 = l1.next;
}
else {
cur.next = l2;
l2 = l2.next;
}
cur = cur.next;
counter ++;
}
cur.next = l1 != null ? l1 : l2;
return pre.next;
}
}

Leetcode: Reorder List && Summary: Reverse a LinkedList的更多相关文章

  1. LeetCode Monotone Stack Summary 单调栈小结

    话说博主在写Max Chunks To Make Sorted II这篇帖子的解法四时,写到使用单调栈Monotone Stack的解法时,突然脑中触电一般,想起了之前曾经在此贴LeetCode Al ...

  2. 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)

    [LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...

  3. 【LeetCode】228. Summary Ranges 解题报告(Python)

    [LeetCode]228. Summary Ranges 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/sum ...

  4. leetcode面试准备:Summary Ranges

    1 题目 Given a sorted integer array without duplicates, return the summary of its ranges. For example, ...

  5. [leetcode]Reorder List @ Python

    原题地址:http://oj.leetcode.com/problems/reorder-list/ 题意: Given a singly linked list L: L0→L1→…→Ln-1→Ln ...

  6. Reorder List 最典型的linkedlist题目

    https://oj.leetcode.com/problems/reorder-list/ Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder ...

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

  8. Leetcode: LFU Cache && Summary of various Sets: HashSet, TreeSet, LinkedHashSet

    Design and implement a data structure for Least Frequently Used (LFU) cache. It should support the f ...

  9. LeetCode 笔记系列六 Reverse Nodes in k-Group [学习如何逆转一个单链表]

    题目:Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. ...

随机推荐

  1. [Tjoi2016&Heoi2016]排序[01序列]

    4552: [Tjoi2016&Heoi2016]排序 Time Limit: 60 Sec  Memory Limit: 256 MBSubmit: 994  Solved: 546[Sub ...

  2. vue--点击事件

    <template> <div id="app"> <p>{{msg}}</p> <input type="text ...

  3. 原生JS实现ajax 发送post请求

    1. [代码]原生JS实现ajax 发送post请求 <script> var oStr = ''; var postData = {}; var oAjax = null; //post ...

  4. 基于pandas python sklearn 的美团某商家的评论分类(文本分类)

    美团店铺评价语言处理以及分类(NLP) 第一篇 数据分析部分 第二篇 可视化部分, 本文是该系列第三篇,文本分类 主要用到的包有jieba,sklearn,pandas,本篇博文主要先用的是词袋模型( ...

  5. codeforces 892 - A/B/C

    题目链接:https://cn.vjudge.net/problem/CodeForces-892A Jafar has n cans of cola. Each can is described b ...

  6. https://validator.w3.org

    https://validator.w3.org/nu/?doc=http%3A%2F%2Fdev.mysql.com%2Fdoc%2Frefman%2F5.7%2Fen%2Fmanual-info. ...

  7. iOS-深入理解(转载)

    RunLoop 是 iOS 和 OS X 开发中非常基础的一个概念,这篇文章将从 CFRunLoop 的源码入手,介绍 RunLoop 的概念以及底层实现原理.之后会介绍一下在 iOS 中,苹果是如何 ...

  8. python MD5步骤

    https://www.cnblogs.com/zipon/p/8340720.html import hashlib def get_token(): md5str = "abc" ...

  9. centos7.2 源码编译安装php7.2.4 apache2.4.37 https证书安装

    一.php7.2.11源码安装 1.下载php7.2.11 wget http://cn2.php.net/downloads.php/php-7.2.11.tar.gz#### 2.安装依赖 yum ...

  10. 敌兵布阵---hud1166(线段树或者树状数组模板)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 线段树中对某一点的值进行改变: #include<iostream> #includ ...