Find intersection point of two LL
Logic:
1) Find the total number of elements in both the LL.
2) Find the difference between number of elements of the two linked list.
3) Move the pointer of that LL which contains more number of elements by the difference.
4) Then move the pointers of both the list and check if nodes present in first LL is same as the node present in second LL.
Time Complexity: O(m+n) if first LL contains m elements and second LL contains n elements.
Space Complexity: O(1)
1) Find the total number of elements in both the LL.
2) Find the difference between number of elements of the two linked list.
3) Move the pointer of that LL which contains more number of elements by the difference.
4) Then move the pointers of both the list and check if nodes present in first LL is same as the node present in second LL.
Time Complexity: O(m+n) if first LL contains m elements and second LL contains n elements.
Space Complexity: O(1)
Comments
Post a Comment