Given a singly linked list LL0→L1→…→Ln-1→Ln,
reorder it to: L0→LnL1→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.* class ListNode {*     int val;*     ListNode next;*     ListNode(int x) {*         val = x;*         next = null;*     }* }*/
public class Solution {public void reorderList(ListNode head) {if(head != null){ListNode stepOne = head, stepTwo = head;while(stepTwo.next != null){if(stepTwo.next != null)stepTwo = stepTwo.next;if(stepTwo.next != null){stepTwo = stepTwo.next;stepOne = stepOne.next;}}if(stepOne.next != null){stepTwo = stepOne.next;stepOne.next = null;ListNode tailOfSecond = stepTwo;stepOne = stepTwo;if(stepTwo.next != null)stepTwo = stepTwo.next;while(stepTwo.next != null){ListNode temp = stepTwo.next;stepTwo.next = stepOne;stepOne = stepTwo;stepTwo = temp;}stepTwo.next = stepOne;tailOfSecond.next = null;}stepOne = head;if(stepTwo != stepOne){while(stepTwo != null){ListNode temp = stepOne.next;stepOne.next = stepTwo;stepOne = stepOne.next;stepTwo = temp;}}}        }
}

  

转载于:https://www.cnblogs.com/averillzheng/p/3552936.html

leetcode--Reorder List相关推荐

  1. 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→ ...

  2. [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 ...

  3. Leetcode: Reorder List

    FROM 思路 1. 将后半段截取下来再倒序, 插入到前半段, 时间复杂度为 o(n) 代码 #include <iostream> using namespace std;struct ...

  4. LeetCode Reorder List

    题意:给出一个链表,重新排列链表,形式为第一个-->倒数第一个->第二个->倒数第二个 思路:将数组分成两半,然后将后半部反转,再拼接 代码发下: class Solution {p ...

  5. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  6. [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 ...

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

  8. 【重点】LeetCode 143. Reorder List

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

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

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

最新文章

  1. OCP读书笔记(5) - 使用RMAN创建备份
  2. CCNA学习笔记--静态路由
  3. Android多线程----异步消息处理机制之Handler
  4. git config设置用户名_一个神奇的工具,实现多人协作,git常用命令详解
  5. 某业务付费统计脚本问题排查
  6. Ocelot统一权限验证
  7. 435. 无重叠区间(贪心经典题+思路+详解)
  8. Codeforces Round #462 (Div. 2), problem: (C) A Twisty Movement (求可以转一次区间的不递增子序列元素只有1,2)...
  9. 力扣-692 前k个高频单词
  10. 数字头盔摄像头是一个智能选项
  11. 时间序列分析:使用Pandas探索能源数据集
  12. 2021美亚杯第七届中国电子数据取证团体赛题目详解write up
  13. python elif invalid syntax_Python错误集锦:if和elif语句提示:SyntaxError: invalid syntax
  14. C语言学习-- 计算机原理及二进制
  15. pandas已知数值怎么找位置_pandas.dataframe中根据条件获取元素所在的位置方法(索引)...
  16. WPS删除粘贴后的[]中括号痕迹
  17. php7.2.3下载,最新PHP编程软件v7.3.2.0 官方版下载地址电脑版-锐品软件
  18. ORACLE+10G+win7下载地址
  19. 我眼里的奇酷手机360OS
  20. 2016年8月2日 星期二 --出埃及记 Exodus 16:2

热门文章

  1. 在kotlin companion object中读取Bean,注入Bean对象
  2. Go 学习笔记(60)— Go 第三方库之 go-redis(初始化 redis、操作 string、操作 list、操作 set、操作 hset)
  3. 2022-2028年中国TPE手套行业市场全景调查及发展策略分析报告
  4. 算法工程师落地_模型的更新升级能力
  5. 输入输出导入导出问题
  6. 一阶逻辑与二阶逻辑的区别一元谓词多元谓词
  7. 聚类和EM算法——K均值聚类
  8. 代码生成codegen
  9. Pipe Utilization管道利用率
  10. Nucleus SE RTOS初始化和启动