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. 使用tensorflow设计的网络模型看不到数据流向怎么办

    首先tensorflow的设计思想就是先把需要用的变量已张量的形式保存, 实际上并没有实质的数值填充. 然后设计网络架构,也仅仅是架构而已, 只能说明数据关系和层与层之间的关系. 真正的数据输入是在主 ...

  2. CMD命令简介

    cmd是command的缩写.即命令行 . 虽然随着计算机产业的发展,Windows 操作系统的应用越来越广泛,DOS 面临着被淘汰的命运,但是因为它运行安全.稳定,有的用户还在使用,所以一般Wind ...

  3. StartWith 测试

    var clientConfiguration = GetConfiguration("couchbase.json"); ClusterHelper.Initialize(cli ...

  4. 14、响应式布局和BootStrap 全局CSS样式知识点总结-part1

    1.什么是响应式布局 响应式布局是Ethan Marcotte在2010年5月份提出的一个概念,这个概念是为解决移动互联网浏览而诞生的. 简而言之,就是一个网站能够兼容多个终端——而不是为每个终端做一 ...

  5. STL学习笔记8 -- 函数对象

    重载函数调用操作符的类,其对象常称为函数对象(function object),即它们是行为类似函数的对象.一个类对象,表现出一个函数的特征,就是通过“对象名+(参数列表)”的方式使用一个类对象,如果 ...

  6. vue - computed

    computed 的作用主要是对原数据进行改造输出.改造输出:包括格式的编辑,大小写转换,顺序重排,添加符号……. 一.格式化输出结果: 我们先来做个读出价格的例子:我们读书的原始数据是price:1 ...

  7. 【EX_BSGS】BZOJ1467 Pku3243 clever Y

    1467: Pku3243 clever Y Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 425  Solved: 238[Submit][Status ...

  8. BZOJ1801 [Ahoi2009]chess 中国象棋 【dp】

    题目 在N行M列的棋盘上,放若干个炮可以是0个,使得没有任何一个炮可以攻击另一个炮. 请问有多少种放置方法,中国像棋中炮的行走方式大家应该很清楚吧. 输入格式 一行包含两个整数N,M,中间用空格分开. ...

  9. 使用PropTypes进行类型检测

    PropTypes 是react提供的用于检验数据类型的typechecking,避免应用越来越大的时候出现意料之外的bug class Greeting extends React.Componen ...

  10. MAC电脑安装Mysql服务器和Navicat for mysql客户端

    1.下载链接 Navicat for mysql客户端 链接: https://pan.baidu.com/s/1dGbzgbR 密码: i43g Mysql服务器 链接: https://pan.b ...