【Hot100】LeetCode—160. 相交链表

目录

  • [1- 思路](#1- 思路)
  • [2- 实现](#2- 实现)
    • [⭐160. 相交链表------题解思路](#⭐160. 相交链表——题解思路)
  • [3- ACM 实现](#3- ACM 实现)


1- 思路

思路

  • 首先想要找到相交点,需要定义连个指针。两个指针一定得是同步的,例如 A 链表 [1,2,3,4,5] ,链表 B 是 [4,5]
    • 1- 指针对齐 :则需要指针1 先移动,移动长度为 lenA - lenB
    • 2- 同时移动 :移动指针1指针2 ,遇到相等则返回相等结点,否则返回 null

2- 实现

⭐160. 相交链表------题解思路

java 复制代码
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode nodeA = headA;
        ListNode nodeB = headB;
        int lenA = 0;
        int lenB = 0;
        while(nodeA!=null){
            lenA++;
            nodeA = nodeA.next;
        }
        while(nodeB!=null){
            lenB++;
            nodeB = nodeB.next;
        }
        if(lenB>lenA) return getIntersectionNode(headB,headA);
        nodeA = headA;
        nodeB = headB;
        // 1. 先移动第一个指针 
        for(int i = 0 ; i < lenA-lenB;i++){
            headA = headA.next;
        }
        while(headA!=null || headB!=null){
            if(headA==headB){
                return headA;
            }
            headA = headA.next;
            headB = headB.next;
        }
        return null;
    }
}

3- ACM 实现

java 复制代码
public class getIntersectionNode {
    public static class ListNode {
        int val;
        ListNode next;
        ListNode(int x) {
            val = x;
            next = null;
        }
    }

    public static ListNode getIntersection(ListNode head1,ListNode head2){
        ListNode curA = head1;
        ListNode curB = head2;
        int len1 = 0;
        int len2 = 0;
        while(curA!=null){
            len1++;
            curA = curA.next;
        }
        while(curB!=null){
            len2++;
            curB = curB.next;
        }
        if(len1<len2) return getIntersection(head2,head1);
        // 遍历
        curA = head1;
        curB = head2;
        while (curA!=null || curB !=null){
            if(curA == curB){
                return curA;
            }
            curA = curA.next;
            curB = curB.next;
        }
        return null;
    }


    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        // 读取第一个链表的节点数量
        int n1 = sc.nextInt();
        ListNode head1 = null, tail1 = null;
        for (int i = 0; i < n1; i++) {
            int val = sc.nextInt();
            ListNode newNode = new ListNode(val);
            if (head1 == null) {
                head1 = newNode;
                tail1 = newNode;
            } else {
                tail1.next = newNode;
                tail1 = newNode;
            }
        }

        // 读取第二个链表的节点数量
        int n2 = sc.nextInt();
        ListNode head2 = null, tail2 = null;
        for (int i = 0; i < n2; i++) {
            int val = sc.nextInt();
            ListNode newNode = new ListNode(val);
            if (head2 == null) {
                head2 = newNode;
                tail2 = newNode;
            } else {
                tail2.next = newNode;
                tail2 = newNode;
            }
        }

        // 读取交点位置
        int intersectionIndex = sc.nextInt();
        if (intersectionIndex >= 0) {
            ListNode intersectionNode = head1;
            for (int i = 0; i < intersectionIndex; i++) {
                if (intersectionNode != null) {
                    intersectionNode = intersectionNode.next;
                }
            }
            if (tail2 != null) {
                tail2.next = intersectionNode;
            }
        }
        System.out.println("结果是"+getIntersection(head1,head2).val);
    }
}
相关推荐
s砚山s3 分钟前
代码随想录刷题——二叉树篇(一)
c++·算法·leetcode
贝塔实验室3 小时前
LDPC 码的构造方法
算法·fpga开发·硬件工程·动态规划·信息与通信·信号处理·基带工程
Greedy Alg3 小时前
LeetCode 287. 寻找重复数
算法
2501_938791223 小时前
逻辑回归与KNN在低维与高维数据上的分类性能差异研究
算法·分类·逻辑回归
南方的狮子先生3 小时前
【深度学习】60 分钟 PyTorch 极速入门:从 Tensor 到 CIFAR-10 分类
人工智能·pytorch·python·深度学习·算法·分类·1024程序员节
报错小能手3 小时前
C++笔记(面向对象)类模板
算法
JJJJ_iii4 小时前
【机器学习10】项目生命周期、偏斜类别评估、决策树
人工智能·python·深度学习·算法·决策树·机器学习
fie88894 小时前
基于MATLAB的LBFGS优化算法实现
算法·matlab
天选之女wow4 小时前
【代码随想录算法训练营——Day50(Day49周日休息)】图论——98.所有可达路径
算法·图论
刀法自然4 小时前
栈实现表达式求值
数据结构·算法·图论