Daily iOS
CS fundamentals and algorithms

Linked list intersection

Two singly linked lists may share their tail nodes. How do you find the first node they actually share (the same instance, not a node with an equal value) in O(n) time and O(1) extra space?

How would an interviewer hear this answer? You'll see whether they'd move on, dig in, or say no, and which sentences would trip you up. Your score doesn't change.
See the short answer

Count the length of each list, advance the longer list's pointer by the difference, then step the two pointers forward together comparing with ===; the first node where they match is the intersection, and if you reach the end without one, it's nil.

Share a question