算法训练营day04

一、24. 两两交换链表中的节点

题目链接:https://leetcode.cn/problems/swap-nodes-in-pairs/description/

文章讲解:https://programmercarl.com/0024.两两交换链表中的节点.html#算法公开课

视频讲解:https://www.bilibili.com/video/BV1YT411g7br

1.1 初见思路

  • 虚拟头节点
  • 三个一组
  • 需返回头节点,所以需要暂存虚拟头节点

1.2 具体实现

1.2.1 方式一:虚拟头节点

java 复制代码
class Solution {
    public ListNode swapPairs(ListNode head) {
        if(head==null){
            return null;
        }
        ListNode pHead = new ListNode();
        pHead.next = head;
        ListNode pre = pHead;
        ListNode n1=head;
        ListNode n2 = head.next;
        ListNode nextH = null;
        while(n1!=null && n2!=null){
            nextH = n2.next;
            pre.next=n2;
            n2.next=n1;
            n1.next=nextH;

            pre=n1;
            n1=nextH;
            if(n1==null){
                break;
            }else{
                n2=n1.next;
            }
            
        }
        return pHead.next;

    }
}

1.2.2 方式二:递归

java 复制代码
// 递归版本
class Solution {
public ListNode swapPairs(ListNode head) {
        // base case 退出提交
        if(head == null || head.next == null) return head;
        // 获取当前节点的下一个节点
        ListNode next = head.next;
        // 进行递归
        ListNode newNode = swapPairs(next.next);
        // 这里进行交换
        next.next = head;
        head.next = newNode;return next;
        }
    } 

1.3 重难点

  • 这里的递归写法应该掌握一下,简单清晰
  • 虚拟头结点主要是注意空指针异常的问题

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

题目链接:https://leetcode.cn/problems/remove-nth-node-from-end-of-list/description/

文章讲解:https://programmercarl.com/0019.删除链表的倒数第N个节点.html#算法公开课

视频讲解:https://www.bilibili.com/video/BV1vW4y1U7Gf

2.1 初见思路

这个题做了很多遍了,快慢指针,不算难

2.2 具体实现

java 复制代码
class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode pre= new ListNode();
        pre.next=head;
        ListNode fast = pre;
        ListNode cur = pre;
        
        for (int i = 0; i <= n; i++) {
            fast = fast.next;
        }
        while(fast!=null){
            cur=cur.next;
            fast=fast.next;
        }
        if(cur.next!=null){
            cur.next = cur.next.next;
        }
        return pre.next;
    }
}

2.3 重难点

  • 快慢指针
  • 用虚拟头节点,同时慢指针也从虚拟头节点开始,这样能方便删除点位

三、 面试题 02.07. 链表相交

题目链接:https://leetcode.cn/problems/intersection-of-two-linked-lists-lcci/description/

文章讲解:https://programmercarl.com/0027.移除元素.html

视频讲解:https://www.bilibili.com/video/BV12A4y1Z7LP

3.1 初见思路

  1. 计算两条链表长度差值k
  2. 两个链表各自一个指针,长链表指针先移动k
  3. 然后两个指针再一起移动,看是否有相同的节点

3.2 具体实现

java 复制代码
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        int sizeA=0;
        int sizeB=0;
        ListNode tA = headA;
        ListNode tB = headB;
        ListNode curA = headA;
        ListNode curB = headB;
        while(tA!=null){
            sizeA++;
            tA=tA.next;
        }
        while(tB!=null){
            sizeB++;
            tB=tB.next;
        }
        if(sizeA>sizeB){
            int diff = sizeA-sizeB;
            //A队列指针先移动diff
            for(int i=0;i<diff;i++){
                curA=curA.next;
            }
        }else{
            int diff=sizeB-sizeA;
            for(int i=0;i<diff;i++){
                curB=curB.next;
            }
        }
        while(curA!=null && curB!=null){
            if(curA==curB){
                return curA;
            }
            else{
                curA=curA.next;
                curB=curB.next;
            }
        }
        return null;
        
    }
}

3.3 重难点

四、 142.环形链表II

题目链接:https://leetcode.cn/problems/linked-list-cycle-ii/description/

文章讲解:https://programmercarl.com/0027.移除元素.html

视频讲解:https://www.bilibili.com/video/BV12A4y1Z7LP

4.1 初见思路

  • 判断是否成环,用快慢指针即可
  • 如何找到环的入口,需要用到初中数据的知识

4.2 具体实现

java 复制代码
public class Solution {
    public ListNode detectCycle(ListNode head) {
        ListNode slow = head;
        ListNode fast = head;
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
            if (slow == fast) {// 有环
                ListNode index1 = fast;
                ListNode index2 = head;
                // 两个指针,从头结点和相遇结点,各走一步,直到相遇,相遇点即为环入口
                while (index1 != index2) {
                    index1 = index1.next;
                    index2 = index2.next;
                }
                return index1;
            }
        }
        return null;
    }
}

4.3 重难点

  • 如何判断入口
相关推荐
2501_924889552 小时前
商超高峰客流统计误差↓75%!陌讯多模态融合算法在智慧零售的实战解析
大数据·人工智能·算法·计算机视觉·零售
jingfeng5143 小时前
C++模板进阶
java·c++·算法
地平线开发者3 小时前
征程 6X | 常用工具介绍
算法·自动驾驶
地平线开发者4 小时前
理想汽车智驾方案介绍 2|MindVLA 方案详解
算法·自动驾驶
艾莉丝努力练剑4 小时前
【C语言16天强化训练】从基础入门到进阶:Day 7
java·c语言·学习·算法
地平线开发者5 小时前
LLM 中评价指标与训练概要介绍
算法·自动驾驶
Ghost-Face5 小时前
关于并查集
算法
flashlight_hi6 小时前
LeetCode 分类刷题:2529. 正整数和负整数的最大计数
python·算法·leetcode
花火|6 小时前
算法训练营day60 图论⑩ Bellman_ford 队列优化算法、判断负权回路、单源有限最短路
算法·图论
2501_924890527 小时前
商超场景徘徊识别误报率↓79%!陌讯多模态时序融合算法落地优化
java·大数据·人工智能·深度学习·算法·目标检测·计算机视觉