首页
mac
it
登录
6mi
u
盘
搜
搜 索
it
剑指Offer--两个链表的第一个公共节点(JS)
剑指Offer--两个链表的第一个公共节点(JS)
mac
2025-09-11
6
题目描述
输入两个链表,找出它们的第一个公共结点。
function FindFirstCommonNode(pHead1, pHead2){ let p1=pHead1; let p2=pHead2; while(p1!==p2){ p1=p1==null?pHead2:p1.next; p2=p2==null?pHead1:p2.next; } return p1; }
用两个指针扫描两个链表,最终两个指针到达null或者到达公共节点
转载请注明原文地址: https://mac.8miu.com/read-506414.html
最新回复
(
0
)