Leetcode 02.07 链表相交(链表)

Leetcode 02.07 链表相交(链表)

很巧妙,这就是将链表的尾端对齐后再一起遍历,这样能满足题目的要求。因为相交之后两个链表到结束的所有节点都一样了,数目也一样。

解法1 尾部对齐

时间复杂度O(M+N)

空间复杂度O(1)

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 Alen = 0, Blen = 0;

        if(headA == null || headB == null) return null;

        // 求两个链表的长度
        while(curA != null){
            curA = curA.next;
            Alen ++;
        }
        while(curB != null){
            curB = curB.next;
            Blen ++;
        }

        curB = headB;
        curA = headA;
        // 【长短尾部对齐】让短的那个的头结点还是其之前的头结点,长的的cur右移(长-短)
        if(Alen > Blen){ 
            for(int i = 0; i < (Alen - Blen); i++){
                curA = curA.next;
            }
        } else if(Alen < Blen){ 
            for(int i = 0; i < (Blen - Alen); i++){
                curB = curB.next;
            }
        }

        // 接下来curA 和 curB 一起向后移动寻找一样的节点
        while(curA != null){
            if(curA == curB){
                return curA;
            }
            curA = curA.next;
            curB = curB.next;
        }
        return null;

    }
}
       
    

解法2:太厉害了,数学归纳推导的方法

在指针 pA 移动了 a+c+b 次、指针 pB 移动了 b+c+a次之后,两个指针会同时到达两个链表相交的节点,该节点也是两个指针第一次同时指向的节点,此时返回相交的节点。

如果两个链表不相交也是一样的道理,当PA指针和PB指针同时遍历m+n后,会同时指向null。

时间复杂度O(1)

空间复杂度O(1)

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) {
        if(headA == null || headB == null) return null;
        ListNode PA = headA;
        ListNode PB = headB;
        // 同时遍历PA,PB,当PA到null则再指向headB,当PB到null则再指向headA
        // 遇到PA = PB 则返回该值
        // 最后同时指向null则返回null
        while(PA != PB){
            if(PA == null) {
                PA = headB;
                continue;
            }
            if(PB == null) {
                PB = headA;
                continue;
            }
            PA = PA.next;
            PB = PB.next;

        }
        if(PA == null) return null;
        else return PA; 
    }
}    
相关推荐
攀小黑1 分钟前
阿里云 使用TST Token发送模板短信
java·阿里云
前端极客探险家6 分钟前
告别卡顿与慢响应!现代 Web 应用性能优化:从前端渲染到后端算法的全面提速指南
前端·算法·性能优化
麦兜*7 分钟前
Spring Boot秒级冷启动方案:阿里云FC落地实战(含成本对比)
java·spring boot·后端·spring·spring cloud·系统架构·maven
自由鬼34 分钟前
正向代理服务器Squid:功能、架构、部署与应用深度解析
java·运维·服务器·程序人生·安全·架构·代理
程序员Xu41 分钟前
【OD机试题解法笔记】连续出牌数量
笔记·算法·深度优先
CoovallyAIHub1 小时前
单目深度估计重大突破:无需标签,精度超越 SOTA!西湖大学团队提出多教师蒸馏新方案
深度学习·算法·计算机视觉
CoovallyAIHub1 小时前
从FCOS3D到PGD:看深度估计如何快速搭建你的3D检测项目
深度学习·算法·计算机视觉
fouryears_234171 小时前
深入拆解Spring核心思想之一:IoC
java·后端·spring
偷偷的卷1 小时前
【算法笔记 day three】滑动窗口(其他类型)
数据结构·笔记·python·学习·算法·leetcode
codervibe1 小时前
使用 Spring Boot + JWT 实现多角色登录认证(附完整流程图)
java·后端