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; 
    }
}    
相关推荐
天上掉下来个程小白5 分钟前
开发环境搭建-06.后端环境搭建-前后端联调-Nginx反向代理和负载均衡概念
java·运维·spring boot·后端·nginx·负载均衡·苍穹外卖
lizz317 分钟前
机器学习中的线性代数:奇异值分解 SVD
线性代数·算法·机器学习
试着生存8 分钟前
java根据List<Object>中的某个属性排序(数据极少,顺序固定)
java·python·list
_星辰大海乀9 分钟前
LinkedList 双向链表
java·数据结构·链表·list·idea
MSTcheng.12 分钟前
【C语言】动态内存管理
c语言·开发语言·算法
不去幼儿园16 分钟前
【启发式算法】Dijkstra算法详细介绍(Python)
人工智能·python·算法·机器学习·启发式算法·图搜索算法
小韩学长yyds20 分钟前
Java调用第三方HTTP接口:从入门到实战
java·开发语言·http
苏十八23 分钟前
JavaEE Servlet02
java·服务器·网络·java-ee·json
serve the people25 分钟前
神经网络中梯度计算求和公式求导问题
神经网络·算法·机器学习
爬菜27 分钟前
异常(5)
java