https://oj.leetcode.com/problems/reorder-list/

将一个链表重新排序,比如 1 2 3 4 5,变成 1 5 2 4 3

1.找到中间节点 mid

2.将链表分成两部分 head,head2

3.将head2 逆序

4.将head head2,再合并成一个链表

class Solution {
public:
void reorderList(ListNode *head) {
//0 elements, 1 elements, 2 elements
if(head == NULL || head != NULL && head->next == NULL || head!= NULL && head->next != NULL && head->next->next == NULL)
return; //find middle
ListNode *end = head;
ListNode *mid = head;
while(end)
{
end = end->next;
if(end && end->next)
{
end = end->next;
mid = mid->next;
}
}
ListNode *head2 = mid->next; //split
mid->next = NULL; //reverse head2
ListNode *itr = head2;
if(head2->next)
{
head2 = head2->next;
itr->next = NULL;
ListNode *tmp = NULL;
while(head2)
{
tmp = head2;
head2 = head2->next;
tmp->next = itr;
itr = tmp;
}
head2 = itr;
} //union
ListNode *dummy = new ListNode();
ListNode *node1 = head;
while(node1)
{
dummy->next = node1;
node1 = node1->next;
dummy = dummy->next;
if(head2)
{
dummy->next = head2;
head2 = head2->next;
dummy = dummy->next;
}
else
break;
}
}
};

LeetCode OJ-- Reorder List **的更多相关文章

  1. [LeetCode OJ] Reorder List—Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…

    For example,Given {1,2,3,4}, reorder it to {1,4,2,3}. /** * Definition for singly-linked list. * str ...

  2. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  3. 【LeetCode OJ】Interleaving String

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

  4. 【LeetCode OJ】Reverse Words in a String

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

  5. LeetCode OJ学习

    一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...

  6. LeetCode OJ 297. Serialize and Deserialize Binary Tree

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  7. 备份LeetCode OJ自己编写的代码

    常泡LC的朋友知道LC是不提供代码打包下载的,不像一般的OJ,可是我不备份代码就感觉不舒服- 其实我想说的是- 我自己写了抓取个人提交代码的小工具,放在GitCafe上了- 不知道大家有没有兴趣 ht ...

  8. LeetCode OJ 之 Maximal Square (最大的正方形)

    题目: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...

  9. LeetCode OJ:Integer to Roman(转换整数到罗马字符)

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

  10. LeetCode OJ:Serialize and Deserialize Binary Tree(对树序列化以及解序列化)

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

随机推荐

  1. 谭浩强C第四版(p141)16.输出以下图案

    运行结果: * *** ***** ******* ***** *** * Press any key to continue #include<stdio.h> int main() { ...

  2. Jin Ge Jin Qu hao UVA - 12563 01背包

    题目:题目链接 思路:由于t最大值其实只有180 * 50 + 678,可以直接当成01背包来做,需要考虑的量有两个,时间和歌曲数,其中歌曲优先级大于时间,于是我们将歌曲数作为背包收益,用时间作为背包 ...

  3. Nordic Collegiate Programming Contest (NCPC) 2016

    A Artwork B Bless You Autocorrect! C Card Hand Sorting D Daydreaming Stockbroker 贪心,低买高卖,不要爆int. #in ...

  4. 笔记-python-standard library-17.2 multiprocessing

    笔记-python-standard library-17.2 multiprocessing 1.      multiprocessing source code:Lib/multiprocess ...

  5. mysql基础查询

    #进阶1:基础查询/*语法:select:查询列表 from 表名; 类似于:System.out.println(打印的东西); 特点:1.查询列表可以是:表中的字段.常量值.表达式.函数2.查询的 ...

  6. Python框架之Django学习笔记(九)

    模型 之前,我们用 Django 建造网站的基本途径: 建立视图和 URLConf . 正如我们所阐述的,视图负责处理一些主观逻辑,然后返回响应结果. 作为例子之一,我们的主观逻辑是要计算当前的日期和 ...

  7. 12、jQuery知识总结-2

    1.避免冲突 jQuery 使用 $ 符号作为 jQuery 的简介方式 <html> <head> <script type="text/javascript ...

  8. IOS应用程序开发流程

    应用程序开发流程 1.IOS开发需要思考的问题 用户是谁?不同应用程序的内容和用户体验大不相同,这取决于想要编写的是什么应用程序,它可能是儿童游戏,也可能是待办事项列表应用程序,又或者是测试自己学习成 ...

  9. UVALive 5099 Nubulsa Expo 全局最小割问题

    B - Nubulsa Expo Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit S ...

  10. jquery中attr和prop的区别介绍

    在高版本的jquery引入prop方法后,什么时候该用prop?什么时候用attr?它们两个之间有什么区别?这些问题就出现了. 关于它们两个的区别,网上的答案很多.这里谈谈我的心得,我的心得很简单: ...