@pranjsha I don't think it is necessary to modify the original code... Since headA = headA.next and headB = headB.next will finally get null if they can't find the intersection, so "return headA" will be null.
And about the 1st condition... "return headA" will also return the intersection point...
@zzg_zzm I didn't read through your post very carefully. But THANKS. I finally figured out why swapping heads makes the solution so fast. The mythology is totally DIFFERENT from "141. Linked List Cycle" that is about two periodic functions must repeat. For this solution, it is just a fancy way to reduce the length diff between two linked lists.
you changed the value of input. The problem only asked to not to change data structure, but I don't think changing the value is a good idea too.
what if the value is not integer? like a string? If a linked list algorithm only works if all value are integer, this is not a really useful algorithm.
the algorithm is actually similar to the method "record every item of A in list, check which one of B is in list", just you added integer together so it didn't take O(n) space, but it really depend on the trick of integer.
I use idea of memory alignment. so, on modern 64-bit systems allocators return memory, divided by 16, and it means, that last 4 bits must always be zero
And we can store some additional data right in pointers.