题目:

Given a singly linked list L: L0 → L1 → … → Ln-1 → Ln

reorder it to: L0 → Ln → L1 → Ln-1 → L2 → Ln-2 → …

Example

Given 1->2->3->4->null, reorder it to 1->4->2->3->null.

题解:

   Spliting the list from the middle into two lists. One from head to middle, and the other from middle to the end. Then we reverse the second list. Finally we merge these two lists

Solution 1 ()

class Solution {
public:
void reorderList(ListNode *head) {
if (!head || !head->next) {
return;
}
ListNode* mid = findMiddle(head);
ListNode* right = reverse(mid->next);
mid->next = nullptr;
merge(head, right);
}
ListNode* findMiddle(ListNode* head) {
ListNode* slow = head;
ListNode* fast = head->next;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
} return slow;
}
ListNode* reverse(ListNode* head) {
if (!head || !head->next) {
return head;
} ListNode* pre = nullptr;
while (head) {
ListNode* tmp = head->next;
head->next = pre;
pre = head;
head = tmp;
}
return pre;
}
void merge(ListNode* left, ListNode* right) {
ListNode* dummy = new ListNode(-);
int idx = ;
while (left && right) {
if (idx % == ) {
dummy->next = left;
left = left->next;
} else {
dummy->next = right;
right = right->next;
}
dummy = dummy->next;
++idx;
}
if (left) {
dummy->next = left;
} else {
dummy->next = right;
}
}
};

  from here

Solution 2 ()

class Solution {
public:
/**
* @param head: The first node of linked list.
* @return: void
*/
void reorderList(ListNode *head) {
// write your code here
if (head == NULL)
return; vector<ListNode*> nodes;
ListNode* iter = head;
while(iter != NULL)
{
nodes.push_back(iter);
iter = iter->next;
} int LEN = nodes.size();
int left = ;
int right = LEN -;
while(left < right)
{
nodes[left]->next = nodes[right];
nodes[right--]->next = nodes[++left];
}
nodes[left]->next = NULL;
}
};

【Lintcode】099.Reorder List的更多相关文章

  1. 【LeetCode】143. Reorder List 解题报告(Python)

    [LeetCode]143. Reorder List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  2. 【lintcode】 二分法总结 I

     二分法:通过O(1)的时间,把规模为n的问题变为n/2.T(n) = T(n/2) + O(1) = O(logn). 基本操作:把长度为n的数组,分成前区间和后区间.设置start和end下标.i ...

  3. 【Leetcode】143. Reorder List

    Question: Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You ...

  4. 【Lintcode】074.First Bad Version

    题目: The code base version is an integer start from 1 to n. One day, someone committed a bad version ...

  5. 【Leetcode_easy】937. Reorder Log Files

    problem 937. Reorder Log Files solution: class Solution { public: vector<string> reorderLogFil ...

  6. 【leetcode】937. Reorder Log Files

    题目如下: You have an array of logs.  Each log is a space delimited string of words. For each log, the f ...

  7. 【LeetCode】937. Reorder Log Files 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 分割和排序 日期 题目地址:https://leet ...

  8. 【LintCode】转换字符串到整数

    问题描述: 实现atoi这个函数,将一个字符串转换为整数.如果没有合法的整数,返回0.如果整数超出了32位整数的范围,返回INT_MAX(2147483647)如果是正整数,或者INT_MIN(-21 ...

  9. 【LintCode】判断一个字符串是否包含另一个字符串的所有字符

    问题描述: 比较两个字符串A和B,确定A中是否包含B中所有的字符.字符串A和B中的字符都是 大写字母. 样例 给出 A = "ABCD" B = "ACD",返 ...

随机推荐

  1. spring-web中的StringHttpMessageConverter简介

    spring的http请求内容转换,类似netty的handler转换.本文旨在通过分析StringHttpMessageConverter 来初步认识消息转换器HttpMessageConverte ...

  2. ThreadLocal,LinkedBlockingQueue,线程池 获取数据库连接2改进

    package com.ctl.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQL ...

  3. leetCode 84.Largest Rectangle in Histogram (最大矩形直方图) 解题思路和方法

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  4. Android防止过快点击造成多次事件

    问题 onClick事件是Android开发中最常见的事件. 比方,一个submitButton.功能是点击之后会提交一个订单, 则一般代码例如以下,当中submitOrder()函数会跳转到下一页进 ...

  5. 【转】【selenium+Python WebDriver】之元素定位不到解决办法

    感谢: 煜妃的<Python+Selenium定位不到元素常见原因及解决办法(报:NoSuchElementException)> ClassName定位报错问题:<[Python] ...

  6. javascript的defer和async(转载)

    http://ued.ctrip.com/blog/?p=3121 我们常用的javascript标签,有两个和性能.js文件下载执行相关的属性:defer和async defer的含义[摘自http ...

  7. iOS开发 僵尸调试

    本文转载至 http://blog.sina.com.cn/s/blog_a843a8850101dxin.html   引自:http://blog.csdn.net/likendsl/articl ...

  8. Python 深入剖析SocketServer模块(二)(V2.7.11)

    五.Mix-In混合类 昨天介绍了BaseServer和BaseRequestHandler两个基类,它们只用与派生,所以贴了它们派生的子类代码. 今天介绍两个混合类,ForkingMix-In 和 ...

  9. Windows 下Nexus搭建Maven私服

    nexus下载地址: http://www.sonatype.org/nexus/archived/#step2top 1. 为什么使用Nexus 如果没有私服,我们所需的所有构件都需要通过maven ...

  10. 20170326 ABAP调用外部webservice 问题

    1.SE80 创建企业服务: 代理生成:出现错误 库处理程序中出现例外 错误的值:未知类型参考ns1:ArrayOfMLMatnrResource 尝试: 一.测试本地文件:---无效 1. 将网址链 ...