【LEETCODE OJ】Reorder List
Problem link:
http://oj.leetcode.com/problems/reorder-list/
I think this problem should be a difficult problem, since it requries three classic algorithms on the single-linked list:
- Split a single-linked list into halves
- Reverse a single-linked list
- Combine two single-linked list into one alternately
Split a single-linked list into havles
The technique of Fast/Slow pointers is commonly used in the single-linked list problems.
Initially, let F and S both be the head of the list. Then each iteration, S goes one step and F goes two steps. The iteration is terminate when F touch the end of the list.
Then, S should be points to the first element of the second half of the list.
Reverse a sinigle-linked list
To reverse a single-linked list in O(n) time, we need scan the list from the second element. For each element, lets say E, we need insert it in the front of head and update the head as E. Because, the insertion may set E->next to the old head, we need an extra pointer to keep track the element after E. Do not forget to set the orginal first (the last after reversing) element's next to NULL (or set it at the begining).
Combine two single-linked list into one alternately
We need three pointers, one used to keep track the combined list, the other two pointers are used for keep the first uncombined element of two lists.
The C++ code is as follows.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
void reorderList(ListNode *head) {
ListNode * h1 = head;
ListNode * h2 = head;
ListNode * p1 = NULL;
ListNode * p2 = NULL;
ListNode * p = head; // Special case: len=0, len=1, len=2
for (int i=0; i<3; i++) {
if (p == NULL) return;
p = p->next;
} // Step 1: split the list into halves
while (h1 != NULL) {
h2 = h2->next;
h1 = h1->next;
if (h1 != NULL) h1 = h1->next;
}
// now h2 is the head of the second half // Step 2: Reverse h2, h2 could not be NULL for len >= 2
p2 = h2->next;
// The head should be the last element after reversing
h2->next = NULL;
// Each time, we move the element of p2 in the front of head
while(p2 != NULL) {
p = p2->next; // Record the next element
p2->next = h2; // Insert p2 in fornt of h2
h2 = p2; // New head is p2
p2 = p; // Go on the next element
} // Step 3: combine h1 and h2
// The first element is always the first
h1 = head->next;
// each time we fill p->next with h1 or h2
p = head;
// Because we know that len(h1) = len(h2) or len(h1) = len(h2) + 1
// so after picking one element from the first half,
// the length of the second half should be not shorter than the first half
// It is sufficient to terminate the combination when p2 is NULL
while(h2 != NULL) {
p->next = h2;
h2 = h2->next;
p = p->next;
if (h1 != NULL) {
p->next = h1;
h1 = h1->next;
p = p->next;
}
}
p->next = NULL;
}
};
【LEETCODE OJ】Reorder List的更多相关文章
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
- 【LeetCode OJ】Reverse Words in a String
Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...
- 【LeetCode OJ】Validate Binary Search Tree
Problem Link: https://oj.leetcode.com/problems/validate-binary-search-tree/ We inorder-traverse the ...
- 【LeetCode OJ】Recover Binary Search Tree
Problem Link: https://oj.leetcode.com/problems/recover-binary-search-tree/ We know that the inorder ...
- 【LeetCode OJ】Same Tree
Problem Link: https://oj.leetcode.com/problems/same-tree/ The following recursive version is accepte ...
- 【LeetCode OJ】Symmetric Tree
Problem Link: https://oj.leetcode.com/problems/symmetric-tree/ To solve the problem, we can traverse ...
- 【LeetCode OJ】Binary Tree Level Order Traversal
Problem Link: https://oj.leetcode.com/problems/binary-tree-level-order-traversal/ Traverse the tree ...
- 【LeetCode OJ】Binary Tree Zigzag Level Order Traversal
Problem Link: https://oj.leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ Just BFS fr ...
- 【LeetCode OJ】Maximum Depth of Binary Tree
Problem Link: https://oj.leetcode.com/problems/maximum-depth-of-binary-tree/ Simply BFS from root an ...
随机推荐
- mint上部署lamp环境
不得不说现在在linux mint上部署lamp很方便,比windows服务器上的asp.net的部署升级都简单. 1 安装MySql sudo apt-get install mysql-serve ...
- IE中JavaScript 的异常处理
错误类型 编程中我们会遇到四种错误:编译错误.逻辑错误.输入/验证错误.运行时错误,但是在JavaScript中我们只能最后两种错误. JavaScript 1.5定义了六种错误类型: EvalErr ...
- jq 中each的用法
jQuery的each方法的几种常用的用法 each()方法能使DOM循环结构简洁,可遍历一维数组.多维数组.DOM, JSON 等等. var arr = [ "one", &q ...
- css背景定位
日期:2015-12-05 背景定位算是才弄明白: background-position:50% 50%; 图片水平和垂直居中.与 background-position:center center ...
- backbonejs mvc框架的增删查改实例
一:开发环境 coffeescript和nodejs需要先安装,没装网上自己查安装步骤. 代码编写环境及esp框架下载: esp框架下载地址:https://github.com/nonocast/e ...
- Javascript 中的false、0、null、undefined和空字符串对象
在Javascript中,我们经常会接触到题目中提到的这5个比较特别的对象——false.0.空字符串.null和undefined.这几个对象很容易用错,因此在使用时必须得小心. 类型检测 我们下来 ...
- 转:Linux 安装 Mysql
前段时间安装了Mysql,但是有些问题,就想把他卸载了,重新安装一个,但是没想到在Linux卸载软件是一个很痛苦的事情. 我的Mysql是用命令的方式安装的,就是上一篇文章用到的那个命令(sudo ...
- [转]Linux下用gcc/g++生成静态库和动态库(Z)
Linux下用gcc/g++生成静态库和动态库(Z) 2012-07-24 16:45:10| 分类: linux | 标签:链接库 linux g++ gcc |举报|字号 订阅 ...
- HTTP 错误 500.22 - Internal Server Error 检测到在集成的托管管道模式下不适用的 ASP.NET 设置
答案:在将WebDataHelper升级到VS2013是出现的这个错误,这个程序使用了URL重写的技术, 解决方法是:需要将重写的配置,迁移到system.webServer配置节中
- spring 标注
1.添加支持标注的spring中的jar包: spring-context.jar spring-context-support.jar 2.在xml中配置命名空间和schema <beans ...