题解 | #两个链表的第一个公共结点#
两个链表的第一个公共结点
https://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46
/** * @author Lu.F * @version 1.0 * @date 2022/10/7 21:36 */ public class Solution { public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) { if (pHead1==null || pHead2==null){ return null; } ListNode p1 = pHead1; ListNode p2 = pHead2; // 问题不能同步移动 // 双循环 while (p1 != null){ // 遍历2结点 while (p2 != null) { if (p2 == p1) { return p2; } p2 = p2.next; } // 重新赋值p2 p2 = pHead2; p1 = p1.next; } // 没有找到 return null; } }