【算法刷题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;

    }
}

总结

暂无

相关推荐
hansang_IR16 分钟前
【题解】洛谷 P4286 [SHOI2008] 安全的航线 [递归分治]
c++·数学·算法·dfs·题解·向量·点积
乐迪信息16 分钟前
乐迪信息:AI摄像机在智慧煤矿人员安全与行为识别中的技术应用
大数据·人工智能·算法·安全·视觉检测
多恩Stone44 分钟前
【3DV 进阶-2】Hunyuan3D2.1 训练代码详细理解下-数据读取流程
人工智能·python·算法·3d·aigc
dragoooon341 小时前
[数据结构——lesson5.1链表的应用]
数据结构·链表
惯导马工2 小时前
【论文导读】IDOL: Inertial Deep Orientation-Estimation and Localization
深度学习·算法
老姜洛克2 小时前
自然语言处理(NLP)之n-gram从原理到实战
算法·nlp
1白天的黑夜12 小时前
哈希表-49.字母异位词分组-力扣(LeetCode)
c++·leetcode·哈希表
CoovallyAIHub3 小时前
基于YOLO集成模型的无人机多光谱风电部件缺陷检测
深度学习·算法·计算机视觉
CoovallyAIHub3 小时前
几十个像素的小目标,为何难倒无人机?LCW-YOLO让无人机小目标检测不再卡顿
深度学习·算法·计算机视觉
怀旧,3 小时前
【C++】19. 封装红⿊树实现set和map
linux·c++·算法