# 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:
        # write code here
        if not pHead1:#==None:#链表1为空链表,直接返回链表2
            return pHead2
        if not pHead2:#==None:#链表2为空链表,直接返回链表1
            return pHead1
        
        head = ListNode(0)#哨兵节点
        cur = head#新链表当前(最后)节点
        while pHead1 and pHead2:#链表1和链表2都还有节点,新链表选取最小节点进行连接
            if pHead1.val<=pHead2.val:
                cur.next, pHead1 = pHead1, pHead1.next
            else:
                cur.next, pHead2 = pHead2, pHead2.next
            cur = cur.next
        
        if pHead1:#链表1还有节点(链表2没),直接把剩余节点续接到新链表后
            cur.next = pHead1
        else:#链表1没有节点,直接把链表2剩余节点续接到新链表后
            cur.next = pHead2
        
        return head.next#返回合并后的新链表