Mid Coding

Find the intersection node of two linked lists (if any) ListNode GetIntersectionNode(ListNode headA, ListNode headB) { if (headA == null || headB == null) return null; ListNode a = headA, b = headB; while (a != b) { a = (a == null) ?

headB : a.next;

b = (b == null) ? headA : b.next;

return a; // either intersection or null

Explanation:

Two pointers traverse both lists; if no intersection, both will reach null simultaneously.

More from C# Programming Tutorial

All questions for this course