题目:

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. CPU调度算法

    批处理系统中的调度算法: *需要考虑的因素: 1. 吞吐量 2. cpu利用率 3. 周转时间 4. 公平性* 1.先来先服务: FCFS: 优点:实现简单 缺点:可能造成周转时间长 2.最短作业优先 ...

  2. python学习(九)python中的变量、引用和对象的关系

    <Think In Java>中说到过"万事万物皆对象",这句话也可以用在Python中. 感觉Python中的变量有点像Javascript中的变量,是弱类型的,但是 ...

  3. linux SPI驱动——gpio模拟spi驱动(三)

    一:首先在我的平台注册platform_device,保证能让spi-gpio.c能执行到probe函数. 1: struct spi_gpio_platform_data { 2: unsigned ...

  4. Linux NAT网络连接权威指南

    [1]准备工作,写在前面 1.1)检查服务(cmd>>services.msc,我用的是VM) 1.2)确保Vmnet8 连接处于启动状态 + 获取ipv4(ipv6)地址 (在网络连接不 ...

  5. Android异步处理二:使用AsyncTask异步更新UI界面

    在<Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面>中,我们使用Thread+Handler的方式实现了异步更新UI界面,这一篇中,我们介绍一种更为简 ...

  6. 【BZOJ4012】[HNOI2015]开店 动态树分治+二分

    [BZOJ4012][HNOI2015]开店 Description 风见幽香有一个好朋友叫八云紫,她们经常一起看星星看月亮从诗词歌赋谈到人生哲学.最近她们灵机一动,打算在幻想乡开一家小店来做生意赚点 ...

  7. EasyPlayer RTSP 安卓Android播放器显示模式设置方法

    一般对于一个播放器,应该支持如下几种显示模式: 等比例,最大化区域显示,不裁剪 等比例,最大区域显示,裁剪 拉伸显示,铺满全屏 要实现这几种显示模式,其实只要对播放控件的布局进行些许调整即可.那Eas ...

  8. mvn deploy

    mvn:deploy在整合或者发布环境下执行,将最终版本的包拷贝到远程的repository,使得其他的开发者或者工程可以共享. 以将ojdbc14传到nexus中的thirdparty为例 一 配置 ...

  9. wimdows安装mongodb,开机启动

    > d: > cd D:\Program Files\MongoDB\Server\3.0\bin > .\mongod --logpath "D:\Program Fil ...

  10. Greedy Function Approximation:A Gradient Boosting Machine

    https://statweb.stanford.edu/~jhf/ftp/trebst.pdf page10 90% to 95% of the observations were often de ...