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 itto {1,4,2,3}.

Considering the following steps:

 * 1. split such list into two list, first and second, according to slow and fast point
 * 2. reverse the second list
 * 3. insert the second list into the first list

coding solution:

/*** Definition for singly-linked list.* class ListNode {*     int val;*     ListNode next;*     ListNode(int x) {*         val = x;*         next = null;*     }* }* */
public class Solution {public void reorderList(ListNode head) {if(head!=null&&head.next!=null){ListNode low=head;//1. split such list into two list, first and second, according to slow and fast pointListNode fast=head;while(fast.next!=null&&fast.next.next!=null){low=low.next;fast=fast.next.next;}ListNode first=head;ListNode second=low.next;low.next=null;second=reverse(second);//2. reverse the second listwhile(second!=null){//3. insert the second list into the first listListNode p1=first.next;ListNode p2=second.next;first.next=second;second.next=p1;first=p1;second=p2;}}}private ListNode reverse(ListNode head){if(head==null||head.next==null)return head;ListNode pre=head;ListNode cur=head.next;while(cur!=null){ListNode nextNode=cur.next;cur.next=pre;pre=cur;cur=nextNode;            }head.next=null;return pre;}
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

转载于:https://www.cnblogs.com/zfyouxi/p/4850687.html

LeetCode Solutions : Reorder List相关推荐

  1. 【重点】LeetCode 143. Reorder List

    LeetCode 143. Reorder List Solution1: 参考网址:http://www.cnblogs.com/grandyang/p/4254860.html 这段代码有值得学习 ...

  2. [LeetCode] 143. Reorder List_Middle tag: Linked List

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

  3. Leetcode Solutions - Part 2

    1. Two Sum 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target  的那 两个 整数,并返回它们的数组下标. 你可以假设每种输入只会对应一 ...

  4. Leetcode Solutions - Part 1

    回溯: 字符串的排列 回溯:78. 子集 给你一个整数数组 nums ,数组中的元素 互不相同 .返回该数组所有可能的子集(幂集). 解集 不能 包含重复的子集.你可以按 任意顺序 返回解集. 示例 ...

  5. 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 ...

  6. leetcode day5 -- Reorder List Linked List Cycle II

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

  7. [Leetcode][JAVA] 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 ...

  8. Leetcode - 143. 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 may not mo ...

  9. [leetcode]143. 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 ...

最新文章

  1. Android Zxing 加入闪光灯功能
  2. 【微信小程序企业级开发教程】页面的生命周期和参数传递
  3. 如何根据字典中值的大小,对字典中的项排序
  4. 利用shell监控cpu、磁盘、内存使用率
  5. speech_to_text_demo powered by IBM!
  6. 基于Vue-SSR优化方案归纳总结
  7. 免费解决vcruntime140.dll文件缺失 - 资源篇
  8. 自由读写配置文件的艺术[java c++ node](二)
  9. 魔兽美服服务器维护,美服《魔兽世界》低人口密度服务器合并计划最新动态
  10. P1059 明明的随机数(C/C++)
  11. 2011年 7月6日の朝会文章 手塚 治虫
  12. 阅读笔记——《R数据可视化手册》肖楠等;主要ggplot2
  13. php yii2模块,Yii2 中关于模块(Modules)的使用及配置
  14. SWAT模型非点源模拟原理
  15. 计算机二级题库access选择题_计算机二级access选择题题库
  16. 不等双11,立减¥3554!戴尔官网撩客服砍价带走高性能电脑,速来!
  17. 数据分析可视化图表mysql_50个最有价值的数据可视化图表
  18. houdini flowmap
  19. 常见的数据结构及其特征
  20. 二项分布的期望方差证明_二项分布的期望和方差

热门文章

  1. c语言死锁算法实验报告,死锁实验报告
  2. python hashlib_python hashlib模块及md5() 、sha()
  3. stm32 PWM输入捕获
  4. (待定系数法)A/B
  5. mysql中pi是什么意思_MySQL 基础知识与常用命令
  6. java编写服务器_java编写一个简单的回射服务器
  7. HTML+CSS+JS实现 ❤️酷炫HUD科幻数据屏幕动画界面❤️
  8. suse linux 创建用户密码,suse linux上创建用户方式
  9. 计算机逻辑判断函数函数知识点,计算机考点条件检测函数IF
  10. java 各种数据类型的互相转换