You have two linked lists: A and B. A and B are co-joined at some node 'c'.
(A)--------
(c) |------- (common part of both linked lists)
(B)--------
Find the the node (c) at which both the linked lists meet / collide.
Solution:
(A)--------
(c) |------- (common part of both linked lists)
(B)--------
Find the the node (c) at which both the linked lists meet / collide.
Solution:
- Traverse both the lists and find their lengths 'a' and 'b' respectively.
- calculate the difference between both lengths: d = |a-b|
- Make two pointers PA and PB for each list.
- Move the pointer of the longer list 'd' steps.
- Then move both the pointers simultaneously until both the pointers become equal, which happens at the node 'c'.
No comments:
Post a Comment