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:

  1. Split a single-linked list into halves
  2. Reverse a single-linked list
  3. 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的更多相关文章

  1. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

  2. 【LeetCode OJ】Reverse Words in a String

    Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...

  3. 【LeetCode OJ】Validate Binary Search Tree

    Problem Link: https://oj.leetcode.com/problems/validate-binary-search-tree/ We inorder-traverse the ...

  4. 【LeetCode OJ】Recover Binary Search Tree

    Problem Link: https://oj.leetcode.com/problems/recover-binary-search-tree/ We know that the inorder ...

  5. 【LeetCode OJ】Same Tree

    Problem Link: https://oj.leetcode.com/problems/same-tree/ The following recursive version is accepte ...

  6. 【LeetCode OJ】Symmetric Tree

    Problem Link: https://oj.leetcode.com/problems/symmetric-tree/ To solve the problem, we can traverse ...

  7. 【LeetCode OJ】Binary Tree Level Order Traversal

    Problem Link: https://oj.leetcode.com/problems/binary-tree-level-order-traversal/ Traverse the tree ...

  8. 【LeetCode OJ】Binary Tree Zigzag Level Order Traversal

    Problem Link: https://oj.leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ Just BFS fr ...

  9. 【LeetCode OJ】Maximum Depth of Binary Tree

    Problem Link: https://oj.leetcode.com/problems/maximum-depth-of-binary-tree/ Simply BFS from root an ...

随机推荐

  1. 笔记7:winfrom的一些知识点(一)

    一.MDI窗体 private void 添加窗体ToolStripMenuItem_Click(object sender, EventArgs e) { Form2 frm1 = new Form ...

  2. 警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property ..

    解决办法是:关闭tomcat,双击eclipse下tomcat服务器,在出来的Tomcat server at localhost页面中找到server options选项,选中其中的选项" ...

  3. 减少HTTP请求之将图片转成二进制并生成Base64编码,可以在网页中通过url查看图片(大型网站优化技术)

    在网站开发过程中,对于页面的加载效率一般都想尽办法求快.那么,怎么让才能更快呢?减少页面请求 是一个优化页面加载速度很好的方法.上一篇博文我们讲解了 “利用将小图标合成一张背景图来减少HTTP请求”, ...

  4. PHP中MySql函数收集

    1.array mysql_fetch_assoc ( resource $result ) 从结果集中取得一行作为关联数组 说明:  返回对应结果集的关联数组,并且继续移动内部数据指针. 参数:re ...

  5. netty4 断线重连

    转载:http://www.tuicool.com/articles/B7RzMbY 一 实现心跳检测 原理:当服务端每隔一段时间就会向客户端发送心跳包,客户端收到心跳包后同样也会回一个心跳包给服务端 ...

  6. BZOJ1507 [NOI2003]Editor

    是一道裸的Splay(反正我不会Splay,快嘲笑我!) 只需要维护在数列上加点删点操作即可,不会写Splay的渣渣只好Orz iwtwiioi大神一下了.(后来发现程序直接抄来了...) 就当我的第 ...

  7. Servlet+JSP+JavaBean开发模式(MVC)介绍

    好伤心...写登陆注册之前看见一篇很好的博文,没有收藏,然后找不到了. 前几天在知乎上看见一个问题,什么时候感觉最无力. 前两天一直想回答:尝试过google到的所有solve case,结果bug依 ...

  8. 铁人系列(2)LA2218

    思路:对于每个人  都会有n-1个半片面  加上x>0,y>0,1-x-y>0(这里的1抽象为总长) 代码是粘贴的  原来写的不见了  orz............ // LA22 ...

  9. 开源性能测试工具--Jmeter介绍+安装

     一.           Apache JMeter介绍 1.       Apache JMeter是什么Apache JMeter 是Apache组织的开放源代码项目,是一个100%纯Java桌 ...

  10. 用PDB库调试Python程序

    Python自带的pdb库,发现用pdb来调试程序还是很方便的,当然了,什么远程调试,多线程之类,pdb是搞不定的. 用pdb调试有多种方式可选: 1. 命令行启动目标程序,加上-m参数,这样调用my ...