#     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
        # 1.将pHead1所在链表的尾部指向pHead2来合并两个链表形成大链表
        # 2.将合并后链表的所有值放入数组中,排序
        # 3.将排序好的数组依次赋值给大链表
        if pHead1 is None:
            return None
        
        cur = pHead1
        while cur.next:
            cur = cur.next
        cur.next = pHead2
        
        point = pHead1
        node_list = []
        while point:
            node_list.append(point.val)
            point = point.next
        print(node_list)
        
        for j in range(len(node_list)-1,0,-1):
            count = 0
            for i in range(j):
                if node_list[i] > node_list[i+1]:
                    node_list[i], node_list[i+1] = node_list[i+1], node_list[i]
                    count += 1
            if count == 0:
                break
        print(node_list)
        
        point2 = pHead1
        i = 0
        while point2:
            point2.val = node_list[i]
            point2 = point2.next
            i += 1
        return pHead1