Leetcode 160. 相交链表

    科技2022-07-11  108

    编写一个程序,找到两个单链表相交的起始节点。 如下面的两个链表:

    在节点 c1 开始相交。

    示例 1:

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

    示例 2:

    输入:intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 输出:Reference of the node with value = 2 输入解释:相交节点的值为 2 (注意,如果两个链表相交则不能为 0)。从各自的表头开始算起,链表 A 为 [0,9,1,2,4],链表 B 为 [3,2,4]。在 A 中,相交节点前有 3 个节点;在 B 中,相交节点前有 1 个节点。 public class Solution { public ListNode getIntersectionNode(ListNode headA, ListNode headB) { //1.特判 if(headA == null || headB == null) return null; //2.定义两个指针 ListNode p1 = headA; ListNode p2 = headB; //3.遍历 while(p1 != p2){ p1 = p1.next; //同步前行 p2 = p2.next; if(p1 == null && p2 == null) return null; if(p1 == null) p1 = headB; if(p2 == null) p2 = headA; } return p1; } }

    来源:https://leetcode-cn.com

    Processed: 0.010, SQL: 8