# 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
dummy = ListNode(None)
head = dummy
cur1 = pHead1
cur2 = pHead2
while cur1 and cur2: # 对两个链表当前节点都存在的情况下,进行遍历赋值
if cur1.val < cur2.val:
dummy.next = cur1
cur1 = cur1.next
else:
dummy.next = cur2
cur2 = cur2.next
dummy = dummy.next
if not cur1: # 有一个为空,下一个节点为另一个非空链表
dummy.next = cur2
if not cur2:
dummy.next = cur1
return head.next # 返回头节点的下一个节点