【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 must do this in-place without altering the nodes' values.
For example,
Given {1,2,3,4}
, reorder it to {1,4,2,3}
.
L0→Ln→L1→Ln-1→L2→Ln-2→…
public void reorderList(ListNode head) {if (head == null || head.next == null)return;// Find the part2;第二部分是从slow.next开始的ListNode slow = head;ListNode fast = head;while (fast.next != null && fast.next.next != null) {slow = slow.next;fast = fast.next.next;}System.out.println("slow"+slow.val);ListNode mid = slow.next;slow.next = null;System.out.println("mid"+mid.val);// 将第二部分翻转;ListNode pre = null;ListNode cur = mid;while (cur != null) {if (cur.next != null) {ListNode next = cur.next;System.out.println("next"+next.val);cur.next = pre;pre = cur;cur = next;} else {cur.next = pre;pre = cur;cur=null;}}System.out.println("pre"+pre.val);// append one by one;ListNode p1 = head;ListNode p2 = pre;while (p2 != null) {ListNode n1 = p1.next;ListNode n2 = p2.next;p1.next = p2;p2.next = n1;p1 = p1.next.next;p1 = n1;p2 = n2;} //printwhile (head != null) {System.out.println(head.val);head = head.next;}}
代码中的一些输出 是为了验证结果的正确性 提交时可删除。leetcode提交版版代码如下:
public void reorderList(ListNode head) {if (head == null || head.next == null)return;// Find the part2;第二部分是从slow.next开始的ListNode slow = head;ListNode fast = head;while (fast.next != null && fast.next.next != null) {slow = slow.next;fast = fast.next.next;}ListNode mid = slow.next;slow.next = null;// 将第二部分翻转;ListNode pre = null;ListNode cur = mid;while (cur != null) {if (cur.next != null) {ListNode next = cur.next;cur.next = pre;pre = cur;cur = next;} else {cur.next = pre;pre = cur;cur=null;}}// append one by one;ListNode p1 = head;ListNode p2 = pre;while (p2 != null) {ListNode n1 = p1.next;ListNode n2 = p2.next;p1.next = p2;p2.next = n1;p1 = p1.next.next;p1 = n1;p2 = n2;}}
转载于:https://www.cnblogs.com/yumiaomiao/p/8479734.html
【Leetcode】143. Reorder List相关推荐
- 【leetcode】937. Reorder Log Files
题目如下: You have an array of logs. Each log is a space delimited string of words. For each log, the f ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】100. 相同的树
题目 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 示例 1: 输入: 1 1/ \ / \2 3 2 3[1,2,3], [1 ...
- 【leetcode】85. Maximal Rectangle 0/1矩阵的最大全1子矩阵
1. 题目 Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1 ...
- 【leetcode】486. Predict the Winner
题目如下: Given an array of scores that are non-negative integers. Player 1 picks one of the numbers fro ...
- 【leetcode】132. Palindrome Partitioning II
题目如下: 解题思路:本题是[leetcode]131. Palindrome Partitioning的升级版,要求的是求出最小cuts,如果用[leetcode]131. Palindrome P ...
- 【leetcode】86. Partition List
题目如下: Given a linked list and a value x, partition it such that all nodes less than x come before no ...
- 【Leetcode】103. 二叉树的锯齿形层次遍历
题目 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行). 例如: 给定二叉树 [3,9,20,null,null,15,7], 3 ...
- 【Leetcode】79.单词搜索
题目 给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻"单元格是那些水平相邻或垂直相邻的单元格.同一个单元格 ...
最新文章
- Android10.0 BroadcastCast广播机制原理
- 如何用手机维护Mysql数据库
- 【PAT乙级】1056 组合数的和 (15 分)
- 神策智能推荐 | 运营后台,你的智能分发“万能助手”
- git push代码出现push rejected错误
- Javascript 常用功能收集-blogjava
- arcgis 分区 属性值_如何使用ArcGIS计算分区河流(管线)总长度
- 带码农《手写Mybatis》进度3:实现映射器的注册和使用
- 推荐系统中的Embedding
- IntelliJ Idea学习笔记004---IDEA中maven没有了
- networkx怎么显示图_如何将标签添加到networkx图形中的节点?
- springboot整合dubbo注解方式(三)
- share 接口的使用
- 推荐十五款APP原型设计工具
- python求三重积分_三重积分的Python数值计算
- 《剪花布条》:从花布条中尽可能剪出几块小饰条
- netkeeper不能建立远程计算机连接,Netkeeper客户端常见问题集锦.doc
- Install SysBench support MySQL and PostgreSQL
- R语言量化:使用WindR下载Wind数据
- 难得五年来第一次暑假没有出海,即使最终没有逃过8月份的CPT外业
热门文章
- install openni2 on ubuntu
- AI学习笔记(十一)CNN之图像识别(下)
- 定时任务管理系统 java_几种任务调度的 Java 实现方法与比较(定时任务)(转)...
- python集合的元素可以是_Python - 集合与元素之集合定义和基本操作方法
- 深度学习自动调参工具,NNI使用
- android 梅花布局,Android相对布局实现各种梅花效果
- android 屏幕方向改变 重新测量,android – 扫描时自动更改屏幕方向(使用ZXING库)...
- 64如何传入后台_如何保证API接口数据安全?
- s8050三极管经典电路_电子电路系统的基本概念有哪些?
- 概率论符号_考研概率论知识点总结1