面试题 02.07. 链表相交——学习笔记

题目:给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表没有交点,返回 null 。

图示两个链表在节点 c1 开始相交:

img

题目数据 保证 整个链式结构中不存在环。

注意,函数返回结果后,链表必须 保持其原始结构 。

示例 1
img

输入:intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
输出:Intersected at '8'
解释:相交节点的值为 8 (注意,如果两个链表相交则不能为 0)。
从各自的表头开始算起,链表 A 为 [4,1,8,4,5],链表 B 为 [5,0,1,8,4,5]。
在 A 中,相交节点前有 2 个节点;在 B 中,相交节点前有 3 个节点。

示例 2
img

输入:intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
输出:Intersected at '2'
解释:相交节点的值为 2 (注意,如果两个链表相交则不能为 0)。
从各自的表头开始算起,链表 A 为 [0,9,1,2,4],链表 B 为 [3,2,4]。
在 A 中,相交节点前有 3 个节点;在 B 中,相交节点前有 1 个节点。

示例 3
img

输入:intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
输出:null
解释:从各自的表头开始算起,链表 A 为 [2,6,4],链表 B 为 [1,5]。
由于这两个链表不相交,所以 intersectVal 必须为 0,而 skipA 和 skipB 可以是任意值。
这两个链表不相交,因此返回 null 。

提示

  • listA 中节点数目为 m
  • listB 中节点数目为 n
  • 0 <= m, n <= 3 * 104
  • 1 <= Node.val <= 105
  • 0 <= skipA <= m
  • 0 <= skipB <= n
  • 如果 listA 和 listB 没有交点,intersectVal 为 0
  • 如果 listA 和 listB 有交点,intersectVal == listA[skipA + 1] == listB[skipB + 1]

题目来源:力扣(LeetCode)链接

题解

  • 自己做的(利用了Map)
    /**
    * 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) {
            //如果有一个链表为空就返回null
            if (headA == null || headB == null) {
                return null;
            }
            //创建一个map集合以键值对的方式存放链表A中的节点
            Map<ListNode, ListNode> nodeMap = new HashMap<>();
            ListNode tempA = headA;
            //遍历A链表,把所有节点加入到map集合中,
            //其中key存放的是当前节点,value存放的是当前节点的下一节点
            while (tempA != null) {
                nodeMap.put(tempA, tempA.next);
                tempA = tempA.next;
            }
            ListNode tempB = headB;
            //遍历B链表
            while (tempB != null) {
                /*
                自己的错误想法
                如果该节点的下一节点等于map中该节点的对应的下一节点,说明找到
                了目标节点,不用这样判断示例1中的两个1节点是不同的节点
                if (tempB.next == nodeMap.get(tempB) && nodeMap.containsKey(tempB)) {
                */
                //这里判断map中是否存在该节点,避免两条链表没有相交的情况
                if (nodeMap.containsKey(tempB)) {
                    //如果满足条件,说明该节点就是目标节点,直接返回
                    return tempB;
                }
                tempB = tempB.next;
            }
            //如果循环结束后没有找到,说明没有相交,返回null
            return null;
        }
    }
    
  • 利用两链表的差值
    /**
    * 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 lenA = 0, lenB = 0;
            while (curA != null) { //求链表A的长度
                lenA++;
                curA = curA.next;
            }
            while (curB != null) { //求链表B的长度
                lenB++;
                curB = curB.next;
            }
            curA = headA;
            curB = headB;
            if (lenB > lenA) { //这里的操作是为了让A链表是两链表中长度最长的链表
                int tempLen = lenA;
                lenA = lenB;
                lenB = tempLen;
                ListNode tempNode = curA;
                curA = curB;
                curB = tempNode;
            }
            int gap = lenA - lenB; //计算长度差
            // 让curA和curB在同一起点上(末尾位置对齐)
            while (gap != 0) {
                curA = curA.next;
                gap--;
            }
            //遍历curA和curB,相同时就为相交节点,直接返回
            while (curA != null) {
                if (curA == curB) {
                    return curA;
                }
                curA = curA.next;
                curB = curB.next;
            }
            //while结束后,说明没有找到相交节点,返回null
            return null;
        }
    }
    
posted @ 2023-03-25 22:20  会飞的笨笨  阅读(7)  评论(0)    收藏  举报