Skip to content

Instantly share code, notes, and snippets.

@mailpraveens
Created March 20, 2014 15:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mailpraveens/9666178 to your computer and use it in GitHub Desktop.
Save mailpraveens/9666178 to your computer and use it in GitHub Desktop.
Find the merge point of two linked list
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