/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
// 最容易想到的是hashmap
// 双指针法
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if(headA == null || headB == null) return null;
ListNode pA = headA; ListNode pB = headB;
while(pA != pB){
pA = pA == null? headB:pA.next;
pB = pB == null? headA:pB.next;
}
return pA;
}
}
相关文章
- 11-21getIntersectionNode