/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
Set<ListNode> set = new HashSet<>();
while(headA != null) {
set.add(headA);
headA = headA.next;
}
while(headB != null) {
if(set.contains(headB)) {
return headB;
} else {
headB = headB.next;
}
}
return null;
}
}
160. 相交链表
最新推荐文章于 2024-05-02 17:55:52 发布