Advertisement
160. Intersection of Two Linked Lists
EasyView on LeetCode
Time: O(m+n)
Space: O(1)
Approach
Two pointers switch to the other list when reaching null; both traverse the same total distance and meet at the intersection.
160.cs
C#
// Approach: Two pointers switch to the other list when reaching null; both
// traverse the same total distance and meet at the intersection.
// Time: O(m+n) Space: O(1)
public class ListNode
{
public int val;
public ListNode next;
public ListNode(int x) { val = x; }
}
public class Solution
{
public ListNode GetIntersectionNode(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;
}
}Advertisement
Was this solution helpful?