Hi,
There are two link list like below:
1--->2--->3--->4--->5--->6--->7--->8
a--->b--->c--->5--->6--->7--->8
Then we need to find out the merging node.
Algo:
1. Calculate the length of first Link List, lets say it is "len1".
2. Calculate the length of second Link List, lets say it is "len2".
3. Find out the greater length out of len1 and len2. in the given example len1 > len2.
4. Find out the positive difference of len1 and len2, in given example |len1 - len2|
5. Now take 1 pointer (ptr1) on the big link list and place it on the |len1 - len2| position from the start.
6. Take 1 pointer (ptr2), and put it on the start of Link list 2.
7. increase both pointer one by one, and check them, if they meet then that is the merging point of two link lists.
Algo with Given example:
1. len1 = 8
2. len2 = 7
3. len1 > len2
4. | len1 - len2 | = 1
5. ptr1 = 2 node of first link list
6. ptr2 = 1 node of second link list
7. in link list1, 3rd-->next and c-->next in link list2 will be pointing to same node, which is 4th node, hence it is the merging node.
Thanks
Maddy
No comments:
Post a Comment