SIMPLE iterative python solution


  • 0
    A
    class Solution(object):
     def mergeTwoLists(self, l1, l2):
            head = temp =  ListNode(0)
            while l1 or l2:
                if l1 and l2:
                    if l1.val < l2.val:
                        temp.next = l1
                        l1 = l1.next
                    else:
                        temp.next = l2
                        l2 = l2.next                
                elif l1:
                    temp.next = l1
                    l1 = l1.next
                elif l2:
                    temp.next = l2
                    l2 = l2.next
                temp = temp.next
            return head.next

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.