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

    }
}

总结

暂无

相关推荐
报错小能手2 小时前
刷题日常 5 二叉树最大深度
算法
ᐇ9593 小时前
Java LinkedList集合全面解析:双向链表的艺术与实战
java·开发语言·链表
Greedy Alg3 小时前
LeetCode 84. 柱状图中最大的矩形(困难)
算法
im_AMBER3 小时前
Leetcode 52
笔记·学习·算法·leetcode
小欣加油3 小时前
leetcode 946 验证栈序列
c++·算法·leetcode·职场和发展
包饭厅咸鱼3 小时前
PaddleOCR----制作数据集,模型训练,验证 QT部署(未完成)
算法
无敌最俊朗@4 小时前
C++ 并发与同步速查笔记(整理版)
开发语言·c++·算法
王哈哈^_^4 小时前
【完整源码+数据集】课堂行为数据集,yolo课堂行为检测数据集 2090 张,学生课堂行为识别数据集,目标检测课堂行为识别系统实战教程
人工智能·算法·yolo·目标检测·计算机视觉·视觉检测·毕业设计
夏鹏今天学习了吗4 小时前
【LeetCode热题100(66/100)】寻找两个正序数组的中位数
算法·leetcode·职场和发展