# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param pHead1 ListNode类
# @param pHead2 ListNode类
# @return ListNode类
#
class Solution:
    def Merge(self, pHead1: ListNode, pHead2: ListNode) -> ListNode:
        if pHead1 == None:
            return pHead2
        if pHead2 == None:
            return pHead1
        i = small = pHead1
        j = pHead2
        if pHead1.val > pHead2.val:
            i = small = pHead2
            j = pHead1

        while i != None and j != None:
            while i.next != None and i.next.val < j.val:
                i = i.next
            tmp = i.next
            i.next = j
            i = tmp
            if tmp == None:
                break
            while j.next != None and j.next.val < i.val:
                j = j.next
            tmp = j.next
            j.next = i
            j = tmp
            if tmp == None:
                break
        return small