# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
a = ListNode(5)
b = ListNode(10)
c = ListNode(20)
e = ListNode(0)
f = ListNode(5)
g = ListNode(21)
h = ListNode(30)
a.next = b
b.next = c
e.next = f
f.next = g
g.next = h
So I have two singly linked lists with heads a and e
I want to merge in ascending order of their values. For now, I want to merge them be iterating through both the linked lists, comparing values UNTIL one of the linked lists reaches None (one of the linked lists are shorter than the other and so one will reach None before the other)
class Solution:
def mergeTwoLists(self, l1, l2):
tempNode = ListNode(0)
returnNode = tempNode
while (l1.next != None) and (l2.next != None):
if l1.val < l2.val:
print("l1.val < l2.val", l1.val, l2.val)
tempNode.next = l1
tempNode = tempNode.next
l1 = l1.next
elif l1.val == l2.val:
print("l1.val == l2.val", l1.val, l2.val)
#If both the values are equal, assign l1's value first
#then make l2's value follow l1's value using tempNode
tempNode.next = l1
tempNode = tempNode.next #Because of the previous statement, after execution of this statement, I'm assuming tempNode.val is now l1.val and tempNode.next = l1.next
#tempNode.next is supposed to be equal to l1.next, instead assign it to l2
tempNode.next = l2
tempNode = tempNode.next #Because of the previous statement, after execution of this statement, I'm assuming tempNode.val is now l2.val and tempNode.next = l2.next
#Increment both l1 and l2
l1 = l1.next
l2 = l2.next
else:
print("l1.val > l2.val", l1.val, l2.val)
tempNode.next = l2
tempNode = tempNode.next
l2 = l2.next
sol = Solution()
sol.mergeTwoLists(a, e)
So here's what I would ideally like to happen but clearly is NOT happening as you will see when you print the statments!
l1.val = 5 > l2.val = 0
increment or move forward with l2!
l1.val = 5 which is == l2.val == 5
they are both equal, so move l1 to it's next AND l2 to it's next
Now, l1.val should be 10 and l2.val should be 21, but
l1.val is being printed as 5 and l2.val is being printed as 21 and stuck in some infinite loop..
Why does l1.val stay at 5 instead of being updated to 10 and why does it stay in this infinite loop instead of one of them reaching their ends according to my while statement?