算法训练营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 重难点

  • 如何判断入口
相关推荐
Funny_AI_LAB20 分钟前
MetaAI最新开源Llama3.2亮点及使用指南
算法·计算机视觉·语言模型·llama·facebook
NuyoahC27 分钟前
算法笔记(十一)——优先级队列(堆)
c++·笔记·算法·优先级队列
jk_10129 分钟前
MATLAB中decomposition函数用法
开发语言·算法·matlab
penguin_bark1 小时前
69. x 的平方根
算法
这可就有点麻烦了1 小时前
强化学习笔记之【TD3算法】
linux·笔记·算法·机器学习
苏宸啊2 小时前
顺序表及其代码实现
数据结构·算法
lin zaixi()2 小时前
贪心思想之——最大子段和问题
数据结构·算法
FindYou.2 小时前
C - Separated Lunch
算法·深度优先
夜雨翦春韭2 小时前
【代码随想录Day30】贪心算法Part04
java·数据结构·算法·leetcode·贪心算法
Kent_J_Truman2 小时前
【平方差 / C】
算法