【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 小时前
四胞胎素数:找出‌个位数分别是 1、3、7、9‌,且‌十位及更高位数字完全相同‌的质数,例如 11、13、17、19
算法·质数·素数·四胞胎素数
Hello.Reader1 小时前
算法基础(十二)——主方法:快速求解常见递归式
算法
小O的算法实验室2 小时前
2026年IEEE TITS,面向按需外卖配送调度的特定问题知识与基于学习元启发式算法,深度解析+性能实测
算法·论文复现·智能算法·智能算法改进
加勒比海带662 小时前
目标检测算法——农林行业数据集汇总附下载链接【Plant】
大数据·图像处理·人工智能·算法·目标检测
洛水水2 小时前
【力扣100题】23. 螺旋矩阵
算法·leetcode·矩阵
影sir2 小时前
不同测试数据下,该如何选择算法
算法·深度优先
潇湘散客2 小时前
CAX软件插件化设计实现牛刀小试
c++·算法·图形学·opengl
速易达网络2 小时前
2026,视觉算法正在经历一场静默革命
算法
WBluuue3 小时前
Codeforces 1094 Div1+2(ABCDE)
c++·算法
TENSORTEC腾视科技3 小时前
腾视科技大模型一体机解决方案:低成本私有化落地,重塑行业智能应用新格局
大数据·人工智能·科技·算法·ai·零售·大模型一体机