Two Pointers1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24/**
* 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) {
if(headA==null || headB==null) return null;
ListNode p1=headA,p2=headB;
while(p1!=p2){
if(p1!=null) p1=p1.next;
else p1=headB;
if(p2!=null) p2=p2.next;
else p2=headA;
}
return p1;
}
}