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. postgresql 触发器

    一.创建事件触发器 1.ddl_command_start - 一个DDL开始执行前被触发: 2.ddl_command_end - 一个DLL 执行完成后被触发: 3.sql_drop -- 删除一 ...

  2. java多线程下如何调用一个共同的内存单元(调用同一个对象)

    /* * 关于线程下共享相同的内存单元(包括代码与数据) * ,并利用这些共享单元来实现数据交换,实时通信与必要的同步操作. * 对于Thread(Runnable target)构造方法创建的线程, ...

  3. SO修改

    FUNCTION Z_SD_SALESORDER_CHANGE1. *"----------------------------------------------------------- ...

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

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

  5. Objective-C( 语法一)

    点语法 点语法的本质是方法调用 成员变量的作用域 @public : 在任何地方都能直接访问对象的成员变量 @private : 只能在当前类的对象方法中直接访问(@implementation中默认 ...

  6. SecureCRT中的ftp文件上传

    原文地址:http://www.blogbus.com/jjuan-flake-logs/59745331.html SecureCRT与SshClient不同的就是,SecureCRT没有图形化的文 ...

  7. input 中的enabled与disabled属性

    <style type="text/css"> *{ padding:; margin:; list-style-type: none; box-sizing:bord ...

  8. MyEclipse8.6 破解以及注册码

    建立JAVA工程文件.将以下Java代码拷贝至类中并执行即可. 注册码: register name: bobo9360013   Serial:oLR8ZC-855550-6065705698041 ...

  9. bzoj 2282: [Sdoi2011]消防

    #include<cstdio> #include<cstring> #include<iostream> #define N 600000 using names ...

  10. PowerMock遇到的问题——4

    当我们在测试一个方法的构造方法的时候,有的时候内部需要new一些对象,这是就需要用到PowerMock.exceptNew(),这个方法,但有时候传的参数有关键字this,比如SAPPublisher ...