Python iterative solution - O(n) - 80ms


  • 1
    G
    # Definition for singly-linked list.
    # class ListNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution:
        # @param {ListNode} l1
        # @param {ListNode} l2
        # @return {ListNode}
        def mergeTwoLists(self, l1, l2):
            if not l1:
                return l2
    
            if not l2:
                return l1
    
            temp = dummyHead = ListNode(0)
    
            while l1 and l2:
                if l1.val < l2.val:
                    temp.next = l1
                    l1 = l1.next
                else:
                    temp.next = l2
                    l2 = l2.next
    
                temp = temp.next
    
            if l1:
                temp.next = l1
    
            if l2:
                temp.next = l2
    
            return dummyHead.next

Log in to reply
 

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