Q: You are given two singly linked lists, such that they start from two different nodes (head) and then, a few nodes down the list, they meet at a common node and then share all the nodes henceforth until the tail. The task is to find the first common node.
Sol:
There are a few ways to solve this. The trivial solution would involve storing all the nodes of the smaller list and traverse each node in the bigger list, comparing each node to the already stored nodes. Instead of storing the nodes you can traverse the whole (smaller) list each time. It is clear however, the each of these are inefficient (time and space)
----------------------------------------------------------------------------------------
Here's another way to do this. Much more efficient but requires modifying the original list
Let list L1 have 'a' number of nodes leading to the intersecting node and 'k' number do nodes afterwards;
a+k=len1 --> equation 1
Let list L2 have 'b' number of nodes leading to the intersecting node and 'k' number do nodes afterwards;
b+k=len2 --> equation 2
You can calculate len1 and len2 by traversing the list
subtracting the above 2 equations you get
a-b= len1-len2; --> equation 3
Now reverse List 2 and then traverse from the head of List 1 and count the number of nodes as you go, you will eventually reach the original head of List 2. Let the length you just computed be len 3
a+b = len 3 --> equation 4
You have 2 equations (3 and 4) and 2 unknowns (a and b)
a and b are the lengths of list from the head of each list to the intersecting node.
You can re-reverse List L2 to revert it to the original state
----------------------------------------------------------------------------------------
Sometimes you do not want to alter the start of list. In multithreaded applications, other threads could be reading from the list and Locking leads to performance issues. The following solution will solve the problem without actually altering the list
First, Traverse the 2 lists and find their lengths (L1 and L2). Traversing the longer list starting from its head until you move (L1-L2) nodes. At this point both the nodes are equidistant from the first intersecting common node. Now Traverse both the list one node at a time and compare the nodes at each step until you find a match. This matching node is the intersecting node.
----------------------------------------------------------------------------------------
Comments
Not sure what to call this method
Let's say the two lists L1 and L2 have lengths Len1 and Len2 and as per the question there exists an intersecting point within them. Let's assume Len1 is shorter than Len2. This assumption is made since we probably could know the lengths of the lists by traversing each of them once to the least.
1. Traverse L2 from its first node to the node at (Len2 - Len1, basically the difference of the two lists). By doing this we are now at the nodes in L1 and L2 from where the length of rest of the nodes in each of them are identical.
2. Compare the pointers to the next nodes in each of these node.
3. If they are equal, we are at the intersecting node and end of sequence, if not, traverse L1 and L2 one node together and go to step 2.
I have not tested this yet. If you find any problems please raise and it will help me.
Regards,
Abani
both r unrealistic and bad
both r unrealistic and bad solution.....do as i say....start with L1 and label each node RED until null...Now start from L2...the first time you get a RED node ...raise alarm...report the count value....
Use a stack: Traverse each
Use a stack:
Traverse each node on the first list, push each node on to the stack.
Then start on the second list, traverse each node, compare each node with popped top of the stack.
When a node on the second list is == the popped top, then you know that that is the intersection point.
Alternate
Alternate solution:
Traverse one of the list. While visiting each node, mark it--like by setting some reserved bit. Then traverse the second list starting from its head. Upon visiting each node, check for the presence of the bit. The first node you visit with such a bit set is the intersection. This approach requires modifying (adding) the original data structure.