【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;
    }
}
相关推荐
mit6.8241 小时前
二维差分+前缀和
算法
民乐团扒谱机1 小时前
自然的算法:从生物进化到智能优化 —— 遗传算法的诗意与硬核“
算法
希望有朝一日能如愿以偿1 小时前
力扣每日一题:仅含1的子串数
算法·leetcode·职场和发展
漂流瓶jz2 小时前
SourceMap数据生成核心原理:简化字段与Base64VLQ编码
前端·javascript·算法
今天的砖很烫2 小时前
ThreadLocal 中弱引用(WeakReference)设计:为什么要 “故意” 让 Key 被回收?
jvm·算法
苏小瀚2 小时前
算法---FloodFill算法和记忆化搜索算法
数据结构·算法·leetcode
苏小瀚2 小时前
算法---二叉树的深搜和回溯
数据结构·算法
诗9趁年华3 小时前
深入分析线程池
java·jvm·算法
九年义务漏网鲨鱼3 小时前
【大模型面经】千问系列专题面经
人工智能·深度学习·算法·大模型·强化学习
源码之家4 小时前
机器学习:基于大数据二手房房价预测与分析系统 可视化 线性回归预测算法 Django框架 链家网站 二手房 计算机毕业设计✅
大数据·算法·机器学习·数据分析·spark·线性回归·推荐算法