SouthLeetCode-打卡24年01月第5周

SouthLeetCode-打卡24年01月第5周

// Date : 2024/01/39 ~ 2024/01/31

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

(1) 题目描述

031 #LeetCode.19. #北岸计划 2024/01/29

(2) 题解代码

Version1.0

class Solution {
   
    public ListNode removeNthFromEnd(ListNode head, int n) {
   
        if(head == null){
    return null; }
        ListNode dummy = new ListNode();
        dummy.next = head;
        ListNode prev = dummy;
        ListNode curr = head;
        ListNode stker = head;
        for(int i=0 ; i<n ; i++){
   
            stker = stker.next;
        }
        while(stker != null){
   
            prev = prev.next;
            curr = curr.next;
            stker = stker.next;
        }
        if(curr == null){
   
            prev.next = null;
        }else{
   
            prev.next = curr.next;
        }
        return dummy.next;
    }
}

(3) 题目描述

  • Version1.0版本中,实践了无脑思路:1.链表转数组 2.操作数组 3.数组转链表 .

032.相交链表

(1) 题目描述

032 #LeetCode.160. #北岸计划 2024/01/30

给你两个单链表的头节点 headAheadB ,请你找出并返回两个单链表相交的起始节点。

如果两个链表不存在相交节点,返回 null

(2) 题解代码

public class Solution {
   

    ListNode getIntersectionNodeHelper(ListNode curr1, ListNode curr2,
                                    ListNode head1, ListNode head2){
   
        curr2 = head1;
        while(curr1 != null){
   
            curr1 = curr1.next;
            curr2 = curr2.next;
        }
        curr1 = curr2;
        curr2 = head2;
        while(curr1 != curr2){
   
            curr1 = curr1.next;
            curr2 = curr2.next;
        }
        return curr1;
    }
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
   
        ListNode currA = headA;
        ListNode currB = headB;
        while(currA != null && currB != null){
   
            currA = currA.next;
            currB = currB.next;
        }
        if(currA != null){
   
            return getIntersectionNodeHelper(currA,currB,headA,headB);
        }else{
   
            return getIntersectionNodeHelper(currB,currA,headB,headA);
        }
    }
}

033.环形链表Ⅱ

(1) 题目描述

033 #LeetCode.142. #北岸计划 2024/01/31

给定一个链表的头节点 head ,返回链表开始入环的第一个节点。 如果链表无环,则返回 null

如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。

为了表示给定链表中的环,评测系统内部使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。

如果 pos-1,则在该链表中没有环。注意:pos 不作为参数进行传递,仅仅是为了标识链表的实际情况。

不允许修改 链表。

(2) 题解代码

public class Solution {
   
    public ListNode detectCycle(ListNode head) {
   
        ListNode dummy = new ListNode();
        dummy.next = head;
        ListNode slow = dummy;
        ListNode fast = dummy;
        boolean isCycle  = false;
        while(fast != null){
   
            slow = slow.next;
            fast = fast.next;
            if(fast != null){
   
                fast = fast.next;
                isCycle = fast == slow ? true : false;
            }
            if(isCycle) break;
        }
        if(isCycle){
   
            fast = dummy;
            while(slow != fast){
   
                slow = slow.next;
                fast = fast.next;
            }
        }
        return fast;
    }
}

相关推荐

  1. SouthLeetCode-24015

    2024-02-03 04:42:02       32 阅读
  2. SouthLeetCode-24014

    2024-02-03 04:42:02       33 阅读
  3. SouthLeetCode-24021

    2024-02-03 04:42:02       29 阅读
  4. SouthLeetCode-24022

    2024-02-03 04:42:02       32 阅读
  5. SouthLeetCode-24023

    2024-02-03 04:42:02       34 阅读
  6. OSINT技术情报精选·202451

    2024-02-03 04:42:02       11 阅读
  7. day01

    2024-02-03 04:42:02       40 阅读
  8. MetaGPT-day01

    2024-02-03 04:42:02       30 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-03 04:42:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-03 04:42:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-03 04:42:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-03 04:42:02       18 阅读

热门阅读

  1. 软件工程知识梳理0-概述

    2024-02-03 04:42:02       32 阅读
  2. 大模型系列课程学习-prompt指令快速入门

    2024-02-03 04:42:02       33 阅读
  3. Sql Server之更改跟踪功能

    2024-02-03 04:42:02       33 阅读
  4. SQL Server 函数参考手册(SQL Server 日期函数)

    2024-02-03 04:42:02       33 阅读
  5. Node Exporter开启tcp相关指标

    2024-02-03 04:42:02       26 阅读
  6. chatchat部署在ubuntu上的坑

    2024-02-03 04:42:02       28 阅读