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

You must do this in-place without altering the nodes' values.

For example,

Given {1,2,3,4}, reorder it to {1,4,2,3}.

先用快慢指针找到链表的中点,然后翻转链表后半部分,再和前半部分组合。

/*** 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) {if (head == NULL || head->next == NULL) return;  ListNode* slow=head,*fast=head;while(fast->next && fast->next->next){slow=slow->next;fast=fast->next->next;}ListNode* list2=slow->next;slow->next=NULL;ListNode* list1=head;ListNode* cur=list2,*temp=list2->next;cur->next=NULL;while(temp){ListNode* tem=temp->next;temp->next=cur;cur=temp;temp=tem;}list2=cur;ListNode* Node1=list1,*node2=list2;while(node2){ListNode* tem1=Node1->next;ListNode* tem2=node2->next;Node1->next=node2;node2->next=tem1;Node1=tem1;node2=tem2;}}
};

Reorder List相关推荐

  1. leetcode之Reorder List

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

  2. R语言dplyr包数据列重排(reorder)实战:把特定数据列移动到第一列、把特定数据列移动到最后一列、数据列多列重排、按照字母顺序重排数据列、把数据列反序

    R语言dplyr包数据列重排(reorder)实战:把特定数据列移动到第一列.把特定数据列移动到最后一列.数据列多列重排.按照字母顺序重排数据列.把数据列反序 目录

  3. R语言ggplot2使用geom_line函数geom_point函数可视化哑铃图、并对哑铃图进行排序(reorder dumbbell plot)

    R语言ggplot2使用geom_line函数geom_point函数可视化哑铃图.并对哑铃图进行排序(reorder dumbbell plot) 目录

  4. ggplot2可视化水平箱图并使用fct_reorder排序数据、使用na.rm处理缺失值(reorder boxplot with fct_reorder)、按照箱图的中位数从小到大排序水平箱图

    ggplot2可视化水平箱图并使用fct_reorder排序数据.使用na.rm处理缺失值(reorder boxplot with fct_reorder).按照箱图的中位数从小到大排序水平箱图(O ...

  5. R对因子变量的等级进行重新排序(Reorder Factor Levels)

    R对因子变量的等级进行重新排序(Reorder Factor Levels) 目录 R对因子变量的等级进行重新排序(Reorder Factor Levels) 因子等级重排

  6. LeetCode:937. Reorder Log Files

    051401 题目 You have an array of logs. Each log is a space delimited string of words. For each log, th ...

  7. Leetcode PHP题解--D54 937. Reorder Log Files

    D54 937. Reorder Log Files 题目链接 937. Reorder Log Files 题目分析 给定一个数组,每一个元素是一条"日志". 每一条日志的第一个 ...

  8. LeetCode - Reorder List

    Reorder List 2014.1.13 22:07 Given a singly linked list L: L0→L1→-→Ln-1→Ln, reorder it to: L0→Ln→L1→ ...

  9. G面经prepare: Reorder String to make duplicates not consecutive

    字符串重新排列,让里面不能有相同字母在一起.比如aaabbb非法的,要让它变成ababab.给一种即可 Greedy: 跟FB面经Prepare task Schedule II很像,记录每个char ...

最新文章

  1. 学习资料:网络回溯分析技术八大应用之安全取证
  2. 实践作业4:Web测试实践(小组作业)每日任务记录3
  3. spring源码分析之定时任务概述
  4. LeetCode 面试题57 - II(剑指offer) 和为s的连续正数序列
  5. matlaba绘制gps星空图_网络图横道图绘制软件 5.0免锁版告别纯手工绘制,修改工作量大!...
  6. Windows 7有用的20条新功能及技巧
  7. CodeForces 1B
  8. 【VS2010学习笔记】【编程实例】 (在Visual Studio中使用C++创建和使用DLL)
  9. Atitiit java通过Exchange协议同步note 记事本 目录 1.1.1. 使用EWS(Exchange Web Service)协议读取邮件、发送邮件 1 最新问题 1 热门问题 1
  10. 【机器学习数学基础】Mathematics for Machine Learning 梳理+习题答案
  11. Visio 画流程图 入门
  12. 前端开发过程中经常遇到的问题以及对应解决方法 (持续更新)
  13. Jade平台的下载与原装
  14. WPS for Linux(ubuntu)字体配置(字体缺失解决办法)
  15. CTU CU CB PU TU
  16. 缓解眼睛疲劳:按摩+复调钢琴曲
  17. uni-app实现微信小程序长按拍视频的功能
  18. 读取NTFS的USN(快速检索文件)
  19. 小甲鱼老师《带你学C带你飞》的后续课程补充
  20. 矩形内方形与长方形的个数

热门文章

  1. WWDC 2011 苹果全球开发者大会【中文】
  2. 使用Silverlight for Embedded开发绚丽的界面(1)
  3. 大数据可视化html模板开源_5个最受工程师欢迎的大数据可视化工具
  4. fir滤波器c++程序_电气信息类专业课程之matlab系统仿真 第三章 滤波器的种类(1)...
  5. LeetCode——Longest Substring Without Repeating Characters
  6. java从Object类型转换成double类型
  7. 使用Troll对ARM Cortex-M处理器进行系统内核调试
  8. Zookeeper,etcd,consul内部机制和分布式锁和选主实现的比较
  9. openSUSE install albertlauncher from source files
  10. Keepalived实现LVS的高可用全解析