# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param head ListNode类 the head node
# @return ListNode类
#
class Solution:
def sortInList(self , head: ListNode) -> ListNode:
# write code here
# 采用分治法:
# 1. 递归的将链表从中间划分为左右两个子链表,直到子链表为空或只有一个节点
# 2. 划分完,根据合并两个子链表的思路进行链表的合并
# # 合并两个子链表
def merge(pH1, pH2):
if not pH1:
return pH2
if not pH2:
return pH1
start = ListNode(-1)
cur = start
while pH1 and pH2:
if pH1.val < pH2.val:
cur.next, pH1 = pH1, pH1.next