题解 | #两个链表的第一个公共结点#
两个链表的第一个公共结点
https://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46
package main import . "nc_tools" /* * type ListNode struct{ * Val int * Next *ListNode * } */ /** * * @param pHead1 ListNode类 * @param pHead2 ListNode类 * @return ListNode类 */ func FindFirstCommonNode(pHead1 *ListNode, pHead2 *ListNode) *ListNode { p := pHead1 q := pHead2 chp := false chq := false for p != nil && q != nil { if p == q { return p } if p.Next == nil { if chp { break } p = pHead2 chp = true } else { p = p.Next } if q.Next == nil { if chq { break } q = pHead1 chq = true } else { q = q.Next } } return nil // write code here }