【LeetCode100】--- 101.重排链表【思维导图+复习回顾】

题目传送门

方法一:(原地修改

题目要求原地修改(不创建新链表节点,直接调整指针)进行合并链表

java 复制代码
class Solution {
    public void reorderList(ListNode head) {
        if(head == null || head.next == null){
            return;
        }
        ListNode slow = head;
        ListNode fast = head;
//寻找链表中心节点
        while(fast.next != null && fast.next.next != null){
            slow = slow.next;
            fast = fast.next.next;
        }
        ListNode head2 = reverseList(slow.next);
        slow.next = null;
        
        ListNode dummy = new ListNode(0);
        ListNode prev = dummy;
        boolean flag = true;

        ListNode h1 = head;
        ListNode h2 = head2;
//合并链表
        while(h1 != null && h2 != null){
            ListNode next1 = h1.next; // 保存h1的下一个节点
            ListNode next2 = h2.next; // 保存h2的下一个节点

            h1.next = h2; // h1连接h2
            h2.next = next1; // h2连接h1的原下一个节点

            h1 = next1; // 移动h1指针
            h2 = next2; // 移动h2指针
        }
    }
//反转链表
    public ListNode reverseList(ListNode slow){
        ListNode dummy = new ListNode(0);
        ListNode prev = dummy;
        
        while(slow != null){
            ListNode next = slow.next;
            slow.next = prev.next;
            prev.next = slow;
            slow = next;
        }
        return prev.next;
    }
}

方法二:(新建链表合并链表)

java 复制代码
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public void reorderList(ListNode head) {
        if(head == null || head.next == null){
            return;
        }

        ListNode slow = head;
        ListNode fast = head;

        while(fast.next != null && fast.next.next != null){
            slow = slow.next;
            fast = fast.next.next;
        }
        ListNode head2 = reverseList(slow.next);
        slow.next = null;
        
        ListNode dummy = new ListNode(0);
        ListNode prev = dummy;

        boolean flag = true;

        ListNode h1 = head;
        ListNode h2 = head2;
        while(h1 != null || h2 != null){
            if(flag == true && h1 != null){
                prev.next = new ListNode(h1.val);
                h1 = h1.next;
                prev = prev.next;
                flag = false;
            }else if(h2 != null){
                prev.next = new ListNode(h2.val);
                h2 = h2.next;
                prev = prev.next;
                flag = true;
            }
        }
        //head = dummy.next;这样不对是因为
        //两个引用彻底指向不同对象,修改head的指向和originalHead无关
        head.next = dummy.next.next;
    }

    public ListNode reverseList(ListNode slow){
        ListNode dummy = new ListNode(0);
        ListNode prev = dummy;
        
        while(slow != null){
            ListNode next = slow.next;
            slow.next = prev.next;
            prev.next = slow;
            slow = next;
        }
        return prev.next;
    }
}
相关推荐
仟濹几秒前
算法打卡day2 (2026-02-07 周五) | 算法: DFS | 3_卡码网99_计数孤岛_DFS
算法·深度优先
驭渊的小故事3 分钟前
简单模板笔记
数据结构·笔记·算法
YuTaoShao18 分钟前
【LeetCode 每日一题】1653. 使字符串平衡的最少删除次数——(解法一)前后缀分解
算法·leetcode·职场和发展
VT.馒头24 分钟前
【力扣】2727. 判断对象是否为空
javascript·数据结构·算法·leetcode·职场和发展
goodluckyaa34 分钟前
LCR 006. 两数之和 II - 输入有序数组
算法
孤狼warrior35 分钟前
YOLO目标检测 一千字解析yolo最初的摸样 模型下载,数据集构建及模型训练代码
人工智能·python·深度学习·算法·yolo·目标检测·目标跟踪
Σίσυφος19001 小时前
PCL法向量估计 之 RANSAC 平面估计法向量
算法·机器学习·平面
xhbaitxl1 小时前
算法学习day39-动态规划
学习·算法·动态规划
I_LPL1 小时前
day23 代码随想录算法训练营 回溯专题2
算法·hot100·回溯算法·求职面试
智者知已应修善业1 小时前
【洛谷P9975奶牛被病毒传染最少数量推导,导出多样例】2025-2-26
c语言·c++·经验分享·笔记·算法·推荐算法