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 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的更多相关文章
- LeetCode Monotone Stack Summary 单调栈小结
话说博主在写Max Chunks To Make Sorted II这篇帖子的解法四时,写到使用单调栈Monotone Stack的解法时,突然脑中触电一般,想起了之前曾经在此贴LeetCode Al ...
- 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)
[LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...
- 【LeetCode】228. Summary Ranges 解题报告(Python)
[LeetCode]228. Summary Ranges 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/sum ...
- leetcode面试准备:Summary Ranges
1 题目 Given a sorted integer array without duplicates, return the summary of its ranges. For example, ...
- [leetcode]Reorder List @ Python
原题地址:http://oj.leetcode.com/problems/reorder-list/ 题意: Given a singly linked list L: L0→L1→…→Ln-1→Ln ...
- Reorder List 最典型的linkedlist题目
https://oj.leetcode.com/problems/reorder-list/ Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder ...
- [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 ...
- 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 ...
- 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. ...
随机推荐
- smali-2.2.4.jar & baksmali-2.2.4.jar
https://bitbucket.org/JesusFreke/smali/downloads/
- Hive学习之数据去重
insert overwrite table store select t.p_key,t.sort_word from ( select p_key, sort_word , row_number( ...
- POJ 2386 Lake Counting(搜索联通块)
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 48370 Accepted: 23775 Descr ...
- 使用jvisuamvm的btrace插件
在之前的文章中写了如何使用btrace来监控运行中的jvm的方法的参数和返回值 jvisualvm中提供了一个btrace插件,我们可以更方便地attach到一个运行中的jvm 更方便地执行和停止bt ...
- (TOJ 4413)IP address
描述 To give you an IP address, it may be dotted decimal IP address, it may be 32-bit binary IP addres ...
- 23种设计模式之组合模式(Composite)
组合模式又称为整体-部分(Part-whole)模式,属于对象的结构模式.在组合模式中,通过组合多个对象形成树形结构以表示整体-部分的结构层次.组合模式对单个对象(即叶子对象)和组合对象(即容器对象) ...
- ElasticSearch 简单入门
英文原文:Getting Started with ElasticSearch 原文链接:http://www.oschina.net/translate/elasticsearch-getting- ...
- Log4net配置之Winform项目
具体方法如下: 一.App.config配置 <?xml version="1.0" encoding="utf-8" ?> <configu ...
- CmD空格转义的三种方法,总有一种会解决问题
CmD空格转义 在cmd中,如果路径中存在空格报错 可以有三种解决方法: 1.将存在空格的路径用双引号包起来,如:"D:/Program Files/xx"; 2.将存在空格的名称 ...
- postgresql----网络地址类型和函数
本人对网络这块实在是搞不清楚,要是能有人推荐一下资料就好了!不知道有没有跟我一样呢?!所以在这里先贴一点从其他地方搞来的一些IPv4的东东. IPv4主要包括一下5类地址 A类: 0 7位 网络号 2 ...