给定一个单链表 LL0→L1→…→Ln-1→Ln ,

将其重新排列后变为: L0→L
nL1→Ln-1→L2→Ln-2→…

你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。

示例 1:

给定链表 1->2->3->4, 重新排列为 1->4->2->3.

示例 2:

给定链表 1->2->3->4->5, 重新排列为 1->5->2->4->3.
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public void reorderList(ListNode head) {
if (head == null || head.next == null || head.next.next == null)
return; ListNode node1 = head;
ListNode node2 = head; while (node2.next != null && node2.next.next != null) {
node2 = node2.next.next;
node1 = node1.next;
} ListNode tail = node2.next == null ? node2 : node2.next; node2 = node1.next;
node1.next = null;
node1 = node2;
node2 = node1.next;
node1.next = null;
while (node2 != null) {
ListNode node3 = node2.next;
node2.next = node1;
node1 = node2;
node2 = node3;
} node1 = head;
node2 = tail; while (node2 != null) {
ListNode temp = node2;
node2 = node2.next;
temp.next = node1.next;
node1.next = temp;
node1 = temp.next;
}
}
}

Q143 重排链表的更多相关文章

  1. L2-022 重排链表 (25 分)

    L2-022 重排链表 (25 分)   给定一个单链表 L​1​​→L​2​​→⋯→L​n−1​​→L​n​​,请编写程序将链表重新排列为 L​n​​→L​1​​→L​n−1​​→L​2​​→⋯.例 ...

  2. L2-022. 重排链表

    L2-022. 重排链表 时间限制 500 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一个单链表 L1→L2→...→Ln-1→Ln,请 ...

  3. pat甲级 团体天梯赛 L2-022. 重排链表

    L2-022. 重排链表 时间限制 500 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一个单链表 L1→L2→...→Ln-1→Ln,请 ...

  4. GPLT天梯赛 L2-022. 重排链表

    L2-022. 重排链表 时间限制 500 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一个单链表 L1→L2→...→Ln-1→Ln,请 ...

  5. Leetcode 143.重排链表

    重排链表 给定一个单链表 L:L0→L1→…→Ln-1→Ln ,将其重新排列后变为: L0→Ln→L1→Ln-1→L2→Ln-2→… 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换. 示 ...

  6. Java实现 LeetCode 143 重排链表

    143. 重排链表 给定一个单链表 L:L0→L1→-→Ln-1→Ln , 将其重新排列后变为: L0→Ln→L1→Ln-1→L2→Ln-2→- 你不能只是单纯的改变节点内部的值,而是需要实际的进行节 ...

  7. LeetCode 重排链表 OPPO笔试

    重排链表 几个关键点: 1. 双指针(快慢指针找中点)(用于反转后一部分) 2. 反转后一部分 (reverse函数) 3. 合并链表 合并的时候在笔试的时候想了一种比我之前想的简单的方法 从slow ...

  8. 【Warrior刷题笔记】143.重排链表 【线性化 || 双指针+翻转链表+链表合并】详细注释

    题目一 力扣143.重排链表 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/reorder-list/ 1.描述 给定一个单链表L的头节点he ...

  9. L2-2 重排链表 (25 分)

    给定一个单链表 L​1​​→L​2​​→⋯→L​n−1​​→L​n​​,请编写程序将链表重新排列为 L​n​​→L​1​​→L​n−1​​→L​2​​→⋯.例如:给定L为1→2→3→4→5→6,则输出 ...

随机推荐

  1. ruby变量

    Ruby 支持五种类型的变量. 一般小写字母.下划线开头:变量(Variable).局部变量的作用域从 class.module.def 或 do 到相对应的结尾或者从左大括号到右大括号 {}. 当调 ...

  2. 安装wampserver后,在www文件夹下面写php文件,而在网页里输入localhost而无法打开php文件时解决办法汇总

    wampserver安装后,在www文件夹下面写入xx.PHP文件,然后在网页里输入localhost:xx.PHP. 你可能会遇到如下三种情况: 情形一:网页上显示空白,按F12,出现404的错误. ...

  3. C# WebApi 过滤器的使用开发接口必备利器

    在WEB Api中,引入了面向切面编程(AOP)的思想,在某些特定的位置可以插入特定的Filter进行过程拦截处理.引入了这一机制可以更好地践行DRY(Don’t Repeat Yourself)思想 ...

  4. 4D(DLG,DRG,DOM,DEM)

    基于“倾斜+LiDAR+车载”的实景三维建模实现:链接 MapGIS数据可不可以做到数据融合 遥感影像

  5. Google Tango service outdated谷歌Tango的服务过时了

    If you device showed "tango service outdated." It means that your Tango Core need to be up ...

  6. Linux 基础教程 31-tcpdump命令-3

        经过前面的学习,tcpdump的用法相信应该都掌握了,今天我们来学习对tcpdump输出内容的学习和了解.我们以第一个示例进行讲解如下所示: IP协议包分析 [root@localhost ~ ...

  7. 软件项目第一个Sprint评分

    第一组 跑男 跑男组他们设计的是极速蜗牛小游戏,他们的界面背景图片做的挺漂亮,现在为止也实现了大部分功能, 但是我没有太听懂他们的游戏规则. 因为蜗牛出发后,每次碰到屏幕边缘后都会有确定的反弹结果,也 ...

  8. FORM 错误:此责任无可用函数。 更改责任或与您的系统管理员联系。

    错误:此责任无可用函数. 更改责任或与您的系统管理员联系. 2014-07-02 12:20:47 分类: Oracle Symptom 访问Help->Diagnostics->Exam ...

  9. TSQL--如何突破PRINT的8000大限

    相信很多DBA都喜欢干的一件事就是拼SQL语句,我也不例外,但是PRINT只能打印4000的Unicode string或8000的Non-unicode string, 这个蛋疼的限制会导致过长的s ...

  10. PostgreSQL递归查询

    原料 --创建组织架构表 create table "Org"( "OrgId" ) primary key, "ParentId" ), ...