Pseudo code :
We can use of Hash table and array sorting method to find out the merging point but below is easier and less complex method of doing the same task.
Ex.
Linked List 1 : l1->l2->l3->l4->->l5->m1->m2->m3 ( Length is : 8)
Linked List 2 : k1->k2->k3->m1->m2->m3 ( Length is : 6)
As you can observe m1 node is the merging point of two linked list.
Steps:
1) Find out the length of both linked list. (In given example linked length is 8 and 6).
2) Get difference of the length and assign it to local variable d. (It is 2 (8-6) in given example )
3) Make the d steps in longer linked list.
4) Step in both list in parallel until links to next node match.
We can use of Hash table and array sorting method to find out the merging point but below is easier and less complex method of doing the same task.
Ex.
Linked List 1 : l1->l2->l3->l4->->l5->m1->m2->m3 ( Length is : 8)
Linked List 2 : k1->k2->k3->m1->m2->m3 ( Length is : 6)
As you can observe m1 node is the merging point of two linked list.
Steps:
1) Find out the length of both linked list. (In given example linked length is 8 and 6).
2) Get difference of the length and assign it to local variable d. (It is 2 (8-6) in given example )
3) Make the d steps in longer linked list.
4) Step in both list in parallel until links to next node match.
No comments:
Post a Comment