文章来源地址https://www.toymoban.com/news/detail-850722.html
import ListNodeInfo.ListNode;
import java.util.HashSet;
import java.util.Set;
public class Problem_160_IntersectionOfTwoLinkedList {
//双指针方法
public ListNode getIntersectionListNode(ListNode headA, ListNode headB){
if(headA == null || headB == null) return null;
ListNode a = headA;
ListNode b = headB;
while (a != b){
a = a == null ? headB : a.next;
b = b == null ? headA : b.next;
}
return a;
}
//利用set方法
public ListNode getIntersectionListNode1(ListNode headA, ListNode headB){
if(headA == null || headB == null)return null;
Set<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;
}
}
文章来源:https://www.toymoban.com/news/detail-850722.html
到了这里,关于力扣 | 160. 相交链表的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!