力扣 中等 143.重排链表

文章目录

题目介绍


题解

java 复制代码
class Solution {
    public void reorderList(ListNode head) {
        ListNode mid = middleNode(head);
        ListNode head2 = reverseList(mid);
        while (head2.next != null) {
            ListNode nxt = head.next;
            ListNode nxt2 = head2.next;
            head.next = head2;
            head2.next = nxt;
            head = nxt;
            head2 = nxt2;
        }
    }

    // 876. 链表的中间结点
    private ListNode middleNode(ListNode head) {
        ListNode slow = head, fast = head;
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }

    // 206. 反转链表
    private ListNode reverseList(ListNode head) {
        ListNode pre = null, cur = head;
        while (cur != null) {
            ListNode nxt = cur.next;
            cur.next = pre;
            pre = cur;
            cur = nxt;
        }
        return pre;
    }
}
相关推荐
亓才孓4 分钟前
[JDBC]批处理
java
春日见5 分钟前
车辆动力学:前后轮车轴
java·开发语言·驱动开发·docker·计算机外设
宋小黑17 分钟前
JDK 6到25 全版本网盘合集 (Windows + Mac + Linux)
java·后端
7哥♡ۣۖᝰꫛꫀꪝۣℋ29 分钟前
Spring-cloud\Eureka
java·spring·微服务·eureka
老毛肚41 分钟前
手写mybatis
java·数据库·mybatis
两点王爷44 分钟前
Java基础面试题——【Java语言特性】
java·开发语言
choke2331 小时前
[特殊字符] Python 文件与路径操作
java·前端·javascript
choke2331 小时前
Python 基础语法精讲:数据类型、运算符与输入输出
java·linux·服务器
岁岁种桃花儿1 小时前
CentOS7 彻底卸载所有JDK/JRE + 重新安装JDK8(实操完整版,解决kafka/jps报错)
java·开发语言·kafka
努力学算法的蒟蒻2 小时前
day79(2.7)——leetcode面试经典150
算法·leetcode·职场和发展