Created
March 20, 2014 15:20
-
-
Save mailpraveens/9666178 to your computer and use it in GitHub Desktop.
Find the merge point of two linked list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def findDivergePointForOfTwoLinkedLists(list1, list2): | |
# Assuming the list class has a method length to give the length of the list | |
lengthList1 = list1.length() | |
lengthList2 = list2.length() | |
differnce = lengthList2 - lengthList1 | |
if(lengthList2>=lengthList1): | |
while(differnce!=0): | |
list2 = list2.next | |
differnce -= 1 | |
else: | |
while (differnce!=0): | |
list1 = list1.next | |
differnce += 1 #Notice the addition, that is because the difference will be negative | |
while(list1 and list2 and list1 != list2): | |
list1 = list1.next | |
list2 = list2.next | |
if(list1 != None and list2 == None): | |
return None | |
if(list1 == None and list2 != None): | |
return None | |
return list1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment