Created
October 5, 2019 07:03
-
-
Save sheldonrobinson/a0caf45583ad5436631aea0d5e86afb9 to your computer and use it in GitHub Desktop.
CideSignal Solution mergeTwoLinkedLists
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
# Singly-linked lists are already defined with this interface: | |
# class ListNode(object): | |
# def __init__(self, x): | |
# self.value = x | |
# self.next = None | |
# | |
def mergeTwoLinkedLists(l1, l2): | |
if l1 == None and l2 == None: | |
return None | |
merged = ListNode(None) | |
ret = merged | |
# prev = merged.value | |
while l1 != None or l2 != None: | |
if l1 != None: | |
if l2 == None and l1.next == None: | |
merged.value = l1.value | |
break | |
if l2 != None: | |
if l1 == None and l2.next == None: | |
merged.value = l2.value | |
break | |
m1 = l2.value if l1 == None else l1.value | |
m2 = l1.value if l2 == None else l2.value | |
val = min(m2 if m1 == None else m1, m1 if m2 == None else m2) | |
merged.next = ListNode(None) | |
merged.value = val | |
merged = merged.next | |
if l1 != None and val >= l1.value: | |
l1=l1.next | |
elif l2 != None and val >= l2.value: | |
l2=l2.next | |
return ret |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment