题解 | #两个链表的第一个公共结点#
两个链表的第一个公共结点
https://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46
/*function ListNode(x){ this.val = x; this.next = null; }*/ function FindFirstCommonNode(pHead1, pHead2) { let stack1 = [], stack2 = []; while (pHead1) { stack1.push(pHead1); pHead1 = pHead1.next; } while (pHead2) { stack2.push(pHead2); pHead2 = pHead2.next; } let len=Math.min(stack1.length,stack2.length) let result=null for(let i=1;i<=len;i++){ if(stack1.at(-i)===stack2.at(-i)){ result=stack1.at(-i) }else{ break } } return result // write code here } module.exports = { FindFirstCommonNode: FindFirstCommonNode, };