【算法刷题day4】Leetcode:24. 两两交换链表中的节点、19.删除链表的倒数第N个节点、面试题 02.07. 链表相交、142.环形链表II

文章目录

草稿图网站
java的Deque

Leetcode 24. 两两交换链表中的节点

题目: 24. 两两交换链表中的节点
解析: 2.5 练习时长两年半

解题思路

借助虚拟头节点,每次处理后面的节点,三种情况:后面为空、后面只有一个接待你不用做;后面有两个非空节点,调转他们。

代码

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 ListNode swapPairs(ListNode head) {
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode curNode = dummy;
        while (curNode.next != null && curNode.next.next != null){
            ListNode tmp = curNode.next;
            curNode.next = tmp.next;
            tmp.next = curNode.next.next;
            curNode.next.next = tmp;

            curNode = curNode.next.next;
        }
        return dummy.next;
    }
}

总结

暂无

Leetcode 19. 删除链表的倒数第 N 个结点

题目: 19. 删除链表的倒数第 N 个结点
解析: 2.5 练习时长两年半

解题思路

fastIdx先走index步,慢的跟上,然后删除节点

代码

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 ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode slowIdx = dummy, fastIdx = dummy;
        for (int i = 0; i < n; i++){
            if (fastIdx == null)
                return dummy.next;
            fastIdx = fastIdx.next;
        }
        while (fastIdx != null && fastIdx.next != null){
            fastIdx = fastIdx.next;
            slowIdx = slowIdx.next;
        }
        slowIdx.next = slowIdx.next.next;
        return dummy.next;
    }
}

总结

题目说index是1到n,所以判断条件冗余了。

可以让fastIdx多走一步,那下面的判断条件可以少一个fast.next!=null

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 ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode slowIdx = dummy, fastIdx = dummy;
        for (int i = 0; i <= n; i++){
            fastIdx = fastIdx.next;
        }
        while (fastIdx != null){
            fastIdx = fastIdx.next;
            slowIdx = slowIdx.next;
        }
        slowIdx.next = slowIdx.next.next;
        return dummy.next;
    }
}

Leetcode面试题 02.07. 链表相交

题目: 面试题 02.07. 链表相交
解析: 2.5 练习时长两年半

解题思路

走到相同的起点,一起往后。

代码

java 复制代码
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode curA = headA;
        ListNode curB = headB;
        int lengthA = 0;
        int lengthB = 0;
        while (curA != null){
            curA = curA.next;
            lengthA++;
        }
        while (curB != null){
            curB = curB.next;
            lengthB++;
        }
        curA = headA;
        curB = headB;
        for (int i = 0; i < lengthA - lengthB; i++)
            curA = curA.next;
        for (int i = 0; i < lengthB - lengthA; i++)
            curB = curB.next;
        while (curA != null){
            if (curA == curB)
                return curA;
            curA = curA.next;
            curB = curB.next;
        }
        return null;
    }
}

总结

暂无

Leetcode 142. 环形链表 II

题目: 142. 环形链表 II
解析: 代码随想录题解

解题思路

相遇一次,再相遇一次就找到了。具体分析过程如下图:

代码

java 复制代码
/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode detectCycle(ListNode head) {
        ListNode slowIdx = head, fastIdx = head;
        while (fastIdx != null && fastIdx.next != null) {
            slowIdx = slowIdx.next;
            fastIdx = fastIdx.next.next;

            //第一次相遇
            if (slowIdx == fastIdx){
                
                //开始第二次相遇
                slowIdx = head;
                while (slowIdx != fastIdx){
                    slowIdx = slowIdx.next;
                    fastIdx = fastIdx.next;
                }
                return slowIdx;
            }
        }
        return null;

    }
}

总结

暂无

相关推荐
IT猿手7 小时前
基于强化学习的多算子差分进化路径规划算法QSMODE的机器人路径规划问题研究,提供MATLAB代码
算法·matlab·机器人
千逐-沐风7 小时前
SMU-ACM2026冬训周报3rd
算法
铉铉这波能秀7 小时前
LeetCode Hot100数据结构背景知识之元组(Tuple)Python2026新版
数据结构·python·算法·leetcode·元组·tuple
晚霞的不甘7 小时前
Flutter for OpenHarmony 实现计算几何:Graham Scan 凸包算法的可视化演示
人工智能·算法·flutter·架构·开源·音视频
㓗冽7 小时前
60题之内难题分析
开发语言·c++·算法
大江东去浪淘尽千古风流人物7 小时前
【VLN】VLN仿真与训练三要素 Dataset,Simulators,Benchmarks(2)
深度学习·算法·机器人·概率论·slam
铉铉这波能秀8 小时前
LeetCode Hot100数据结构背景知识之字典(Dictionary)Python2026新版
数据结构·python·算法·leetcode·字典·dictionary
蜡笔小马8 小时前
10.Boost.Geometry R-tree 空间索引详解
开发语言·c++·算法·r-tree
我是咸鱼不闲呀8 小时前
力扣Hot100系列20(Java)——[动态规划]总结(下)( 单词拆分,最大递增子序列,乘积最大子数组 ,分割等和子集,最长有效括号)
java·leetcode·动态规划
唐梓航-求职中8 小时前
编程-技术-算法-leetcode-288. 单词的唯一缩写
算法·leetcode·c#