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; 
    }
}    
相关推荐
tao35566729 分钟前
【Python刷力扣hot100】42. Trapping Rain Water
开发语言·python·leetcode
Miraitowa_cheems34 分钟前
LeetCode算法日记 - Day 88: 环绕字符串中唯一的子字符串
java·数据结构·算法·leetcode·深度优先·动态规划
黑云压城After1 小时前
vue2实现图片自定义裁剪功能(uniapp)
java·前端·javascript
B站_计算机毕业设计之家1 小时前
python电商商品评论数据分析可视化系统 爬虫 数据采集 Flask框架 NLP情感分析 LDA主题分析 Bayes评论分类(源码) ✅
大数据·hadoop·爬虫·python·算法·数据分析·1024程序员节
小白菜又菜2 小时前
Leetcode 1518. Water Bottles
算法·leetcode·职场和发展
长存祈月心2 小时前
Rust Option 与 Result深度解析
算法
zcl_19912 小时前
记一次ThreadLocal导致的生产事故
java
RoboWizard2 小时前
怎么判断我的电脑是否支持PCIe 5.0 SSD?Kingston FURY Renegade G5
java·spring·智能手机·电脑·金士顿
杭州杭州杭州3 小时前
机器学习(3)---线性算法,决策树,神经网络,支持向量机
算法·决策树·机器学习
毕设源码-钟学长3 小时前
【开题答辩全过程】以 儿童游泳预约系统为例,包含答辩的问题和答案
java·eclipse