【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;
    }
}
相关推荐
想逃离铁厂的老铁1 分钟前
Day42 >> 188、买卖股票的最佳时机IV + 309.最佳买卖股票时机含冷冻期 + 714.买卖股票的最佳时机含手续费
算法·leetcode·职场和发展
wu_asia3 分钟前
方阵对角线元素乘积计算
数据结构·算法
想逃离铁厂的老铁34 分钟前
Day43 >> 300.最长递增子序列 + 674. 最长连续递增序列+ 718. 最长重复子数组
数据结构·算法
Yzzz-F35 分钟前
P6648 [CCC 2019] Triangle: The Data Structure [st表]
算法
LateFrames1 小时前
泰勒级数:从 “单点” 到 “理论与实践的鸿沟”
学习·算法
武帝为此1 小时前
【RC4加密算法介绍】
网络·python·算法
宵时待雨1 小时前
数据结构(初阶)笔记归纳4:单链表的实现
c语言·开发语言·数据结构·笔记·算法
wm10431 小时前
代码随想录第三天 链表
数据结构·链表
BLSxiaopanlaile1 小时前
关于子集和问题的几种解法
数据结构·算法·剪枝·回溯·分解
狐571 小时前
2026-01-17-LeetCode刷题笔记-3047-求交集区域内的最大正方形面积
笔记·算法·leetcode