@perennial_noob said in Python solutions (iteratively, recursively, iteratively in-place).:

Can someone please help me understand why I don't get the complete merged list? I used iterative method. I inserted print statements to make sure I see what I expect. I am even returning the HEAD that I saved at the beginning.

'''

class Solution(object):

def mergeTwoLists(self, l1, l2):`if l1 == None and l2 == None: return None if l1 == None: return l2 if l2 == None: return l1 my_first = l3 = None while l1 != None and l2 != None: if l1.val <= l2.val: print("If - putting %s" % l1.val) if l3 == None: print("If - no L3") l3 = ListNode(l1.val) my_first = l3 else: l3.next = ListNode(l1.val) l1 = l1.next else: print("Else - putting %s" % l2.val) if l3 == None: print("Else - no L3") l3 = ListNode(l2.val) my_first = l3 else: l3.next = ListNode(l2.val) l2 = l2.next if l1 == None: print("rest - l2") l3.next = l2 else: print("rest - l1") l3.next = l1 return my_first`

'''

I realized that I was overwriting L3 pointer. I needed: l3 = l3.next

Refactored code:

'''

class Solution(object):

def mergeTwoLists(self, l1, l2):

```
if l1 == None and l2 == None:
return None
if l1 == None:
return l2
if l2 == None:
return l1
my_first = l3 = None
while l1 != None and l2 != None:
if l1.val <= l2.val:
if l3 == None:
l3 = ListNode(l1.val)
else:
l3.next = ListNode(l1.val)
l3 = l3.next
l1 = l1.next
else:
if l3 == None:
l3 = ListNode(l2.val)
else:
l3.next = ListNode(l2.val)
l3 = l3.next
l2 = l2.next
if my_first == None:
my_first = l3
#print(l3)
if l1 == None:
l3.next = l2
else:
l3.next = l1
return my_first
```

'''