0
点赞
收藏
分享

微信扫一扫

Leetcode160. 相交链表

菜头粿子园 2022-09-17 阅读 160


题目传送:​​https://leetcode.cn/problems/intersection-of-two-linked-lists/submissions/​​

运行效率

Leetcode160. 相交链表_java


代码如下

public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
HashSet<ListNode> set = new HashSet<>();
while (headA != null) {
set.add(headA);
headA = headA.next;
}

while (headB != null) {
if (set.contains(headB)) {
return headB;
}
headB = headB.next;
}
return null;
}


举报

相关推荐

0 条评论