21.Merge Two Sorted Lists

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
        
        if l1 == None:
            if l2 == None:
                return None
            else:
                return l2
        if l2 == None:
            if l1 != None:
                return l1
        node = ListNode(0)
        head_node = node
        while 1:
            if l1.val < l2.val:
                node.next = l1
                node = node.next
                l1 = l1.next
            else:
                node.next = l2
                node = node.next
                l2 = l2.next
            if l1 == None:
                node.next = l2
                break
            if l2 == None:
                node.next = l1
                break

        head_node = head_node.next
        return head_node
原文地址:https://www.cnblogs.com/luo-c/p/12857157.html