Enter two linked lists to find their first common node.

Here are two linked lists:

The intersection begins at node C1.

If a is null, the entire list has been traversed to the end of the list. Let A and B traverse the same distance, each going through A separate head and A common tail, so that when they are equal they must be the first common node. As shown in the picture above, A runs through 5 and B runs to the common 4 position. A then runs from B's headB, and B also runs to the end from headA. Since they run the common tail once and the head once respectively, when the nodes are equal, it is the node 8. Public class Solution {public ListNode getIntersectionNode(ListNode headA, ListNode headB) { ListNode a = headA; ListNode b = headB; while(a ! = b) { a = a ! = null ? a.next:headB; b = b ! = null ? b.next:headA; } return a; }}Copy the code

Reference: leetcode-cn.com/problems/li…