题解 | #合并两个排序的链表#
合并两个排序的链表
https://www.nowcoder.com/practice/d8b6b4358f774294a89de2a6ac4d9337
# 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 #null if pHead1 is None: return pHead2 if pHead2 is None: return pHead1 cur1 = pHead1 cur2 = pHead2 if cur1.val <= cur2.val: head = cur1 if cur1.next is None: head.next = cur2 return head else: cur1 = cur1.next n = cur2 else: head = cur2 if cur1.next is None: head.next = cur2 return head else: cur2 = cur2.next n = cur1 head0 = head while n: if cur1.val <= cur2.val: if cur1.next is None: head.next = cur1 cur1.next = cur2 return head0 else: tem = cur1.next head.next = cur1 head = cur1 cur1 = tem else: if cur2.next is None: head.next = cur2 cur2.next = cur1 return head0 else: tem = cur2.next head.next = cur2 head = cur2 cur2 = tem return head0
超级笨办法