力扣-Hot100-链表其一【算法学习day.34】

前言

###我做这类文档一个重要的目的还是给正在学习的大家提供方向(例如想要掌握基础用法,该刷哪些题?)我的解析也不会做的非常详细,只会提供思路和一些关键点,力扣上的大佬们的题解质量是非常非常高滴!!!


习题

1.相交链表

题目链接: 160. 相交链表 - 力扣(LeetCode)

题面:

**基本分析:**我们假设公共链表长度为c,A链表前面长度为a,B链表前面长度为b,我们假设指针p1指向headA,指针p2指向headB,那么p1到达如上图的相交节点c1,需要走的步数为a+b+c,p2同理,所以我们可以先让p1走到尽头,然后让p1指向headB继续走,p2同理,如果两指针相遇,相遇点就是相交节点

代码:

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 n1 = new ListNode();
       ListNode n2 = new ListNode();
       n1 = headA;
       n2 = headB;
       int flag = 0;
       while(n1!=n2){
        n1 = n1.next;
        n2 = n2.next;
        if(n1==null&&flag<3){
            n1 = headB;
            flag++;
        }
        if(n2==null&&flag<3){
            n2 = headA;
            flag++;
        }
        if(flag==3)break;
       }
       if(flag==3)return null;
       return n1;
    }
}

2.回文链表

题目链接: 234. 回文链表 - 力扣(LeetCode)

题面:

**基本分析:**我是通过先遍历一遍把值存起来然后判断的暴力做法,不符合题目要求,可以看看力扣的大佬题解

代码:

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 boolean isPalindrome(ListNode head) {
        int[] arr = new int[100005];
        int count = 0;
        for(ListNode i = head;i!=null;i=i.next){
            arr[count++] = i.val;
        }
        int l =0;
        int r = count-1;
        while(l<=r){
            if(arr[l]!=arr[r])return false;
            l++;
            r--;
        }
        return true;
    }
}

3.环形链表

题目链接: 141. 环形链表 - 力扣(LeetCode)

题面:

**基本分析:**因为题目限制链表长度最大为10000,所以可以一直遍历来暴力判断

代码:

java 复制代码
/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public boolean hasCycle(ListNode head) {
         if(head==null)return false;
         int count = 0;
         ListNode node = new ListNode();
         node = head;
         while(true){
            node = node.next;
            if(node==null)return false;
            count++;
            if(count==10005)break;
         }
         return true;
    }
}

后言

上面是力扣Hot100的链表专题,下一篇是该专题的其他题目,希望有所帮助,一同进步,共勉!

相关推荐
春风解人意1 小时前
从零开始学习嵌入式P32----网络基础之TCP
c语言·网络·学习·tcp/ip
魏 无羡2 小时前
webclient
java·webclient
神仙别闹2 小时前
基于 C++ 实现两个有序链表序列的交集
java·c++·链表
传奇开心果编程3 小时前
【Rust入门知识点学与练】第21课:Trait 进阶 Advanced Traits
开发语言·学习·rust
水巷石子3 小时前
备考系统架构设计师第一天
学习·架构·软考·设计·考试
swordbob3 小时前
ReentrantLock 与 AQS 完整学习手册
java·开发语言
政企项目老覃4 小时前
大模型幻觉治理与自动评测:金融风控场景的落地实践
人工智能·算法·机器学习
是隼人4 小时前
buuctf-pwn inndy_echo(32位fmt)题解(学习过程持续更新)
c语言·学习·安全·pwn入门·ctf入门
淡海水4 小时前
08-03-不可变-ImmutableDictionary-TKey-TValue-与ImmutableHashSet-T-持久化哈希树
数据结构·算法·c#·哈希算法·dictionary·immutable