/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { //TC = same as previous O(m+n) public ListNode getIntersectionNode(ListNode headA, ListNode headB) { ListNode tempa = headA; ListNode tempb = headB; //using a cycle trick while(tempa != tempb){ tempa = tempa==null ? headB : tempa.next; tempb = tempb==null ? headA : tempb.next; } return tempa; } }