【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 ...
随机推荐
- Remove Duplicates from Sorted List II [LeetCode]
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- 5月11日 ArrayList集合复习、特殊集合、枚举类型
一.ArrayList集合复习 //定义 ArrayList al = new ArrayList(); //添加元素 al.Add(); //插入元素 al.Insert(,); //查看个数 in ...
- LeetCode 423. Reconstruct Original Digits from English——学会观察,贪心思路
Given a non-empty string containing an out-of-order English representation of digits 0-9, output the ...
- [转]玩转Google开源C++单元测试框架Google Test系列
gtest的官方网站是: http://code.google.com/p/googletest/ 从官方的使用文档里,你几乎可以获得你想要的所有东西 http://code.google.com/p ...
- 解决一个报表EdmFunction报错问题
最近测试组提了一个bug,说是某个报表点击查询报错,查看错误log,错误信息如下. 类型"Ticket.Data.SqlFuns"上指定的方法"Boolean C ...
- SQL 调试:无法启动 T-SQL 调试。未能附加到 SQL Server 进程
将 Windows 登录帐户添加为 sysadmin 已经具有 sysadmin 特权的用户必须执行以下命令: sp_addsrvrolemember 'Domain\Name', 'sysadmin ...
- 设置ASP.NET MVC站点默认页为html页
问题由来 部署了一个Asp.Net MVC的站点,其功能只是作为移动端的服务器,服务器空间里面除了CMS以外就没有其他的页面了.这对于我们来说确实是有点浪费了. 可以放点静态的啥小东西放在上面玩一玩. ...
- linux :TOP命令及参数解析
第二行 分别显示:total进程总数. running正在运行的进程数. sleeping睡眠的进程数.stopped停止的进程数. zombie僵尸进程数. 第三行 分别显示: %us 用户空间占用 ...
- S1 :闭包
闭包是指有权访问另一个函数作用域中的变量的函数.创建闭包的常见方式,就是在一个函数内部创建另一个函数,以createComparisonFunction()函数为例 function createCo ...
- [开发笔记]-FireWorks常用操作快捷键
一.工具快捷键 指针.选择后方对象[V],[0] 部分选定[A],[1] 选取框.椭圆选取框[M] 套索.多边形套索[L] 裁剪.导出区域[C] 魔术棒[W] 线条工具[N] 钢笔工具[P] 矩形.圆 ...