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; 
    }
}    
相关推荐
Code_Solitude12 小时前
C语言动态内存 + 柔性数组 + 数组名退化
c语言·算法·柔性数组
上海潭龙东海科技有限公司12 小时前
客运站管控之动态孪生技术方案(新增一屏管控模块
数据仓库·人工智能·算法·重构·动态规划
文丑颜不良啊13 小时前
LeetCode2821~2840题目解析
java·leetcode
apcipot_rain13 小时前
C++转Java日记(10)
java·开发语言
简 洁 冬冬14 小时前
泛型方法写法全面辨析:从「能编译」到「能跑」到「能用」
java·泛型
YLCHUP14 小时前
【思维&图论建模】题解:债权(debt)_图论_建模_思路转化_思维题目_C++算法竞赛
c++·笔记·算法·数学建模·贪心算法·深度优先·图论
LGL6030A14 小时前
Java学习历程31——初识Spring Boot
java·spring boot·学习
思麟呀15 小时前
背包DP(01背包+完全背包)
c++·算法
笑容掩饰我的失落16 小时前
区块链从小白到精通(七):Solidity 语法精讲——数据类型、函数、修饰器与事件
算法·区块链·php·智能合约·哈希算法
Lansonli16 小时前
大数据Spark(八十八):Spark任务资源之Standalone集群测试
java·服务器·spark