题解 | #两个链表的第一个公共结点#
两个链表的第一个公共结点
https://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46
/*class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param pHead1 ListNode类
* @param pHead2 ListNode类
* @return ListNode类
*/
export function FindFirstCommonNode(pHead1: ListNode, pHead2: ListNode): ListNode {
// write code here
let cur1 = pHead1, cur2 = pHead2
while(cur1 !== cur2) {
cur1 = cur1 === null ? pHead2 : cur1.next
cur2 = cur2 === null ? pHead1 : cur2.next
}
return cur1
}

查看20道真题和解析