【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;
    }
}
相关推荐
智者知已应修善业3 小时前
【求中位数】2024-1-23
c语言·c++·经验分享·笔记·算法
地平线开发者4 小时前
PTQ 量化数值范围与优化
算法·自动驾驶
sali-tec4 小时前
C# 基于halcon的视觉工作流-章68 深度学习-对象检测
开发语言·算法·计算机视觉·重构·c#
测试人社区-小明4 小时前
智能弹性伸缩算法在测试环境中的实践与验证
人工智能·测试工具·算法·机器学习·金融·机器人·量子计算
罗西的思考5 小时前
【Agent】MemOS 源码笔记---(5)---记忆分类
人工智能·深度学习·算法
qq_433554548 小时前
C++数位DP
c++·算法·图论
AshinGau8 小时前
Softmax 与 交叉熵损失
神经网络·算法
似水এ᭄往昔8 小时前
【C++】--AVL树的认识和实现
开发语言·数据结构·c++·算法·stl
栀秋6668 小时前
“无重复字符的最长子串”:从O(n²)哈希优化到滑动窗口封神,再到DP降维打击!
前端·javascript·算法
xhxxx8 小时前
不用 Set,只用两个布尔值:如何用标志位将矩阵置零的空间复杂度压到 O(1)
javascript·算法·面试