解法一:(哈希集合)利用HashSet保存一个链表的值,循环另一个列表,在HashSet中寻找该值。
/*** 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<>();ListNode temp_node = headA;while(temp_node != null){set.add(temp_node);temp_node = temp_node.next;}temp_node = headB;while(temp_node != null){if(set.contains(temp_node)){return temp_node;}temp_node = temp_node.next;}return null;}
}