题解 | #合并两个排序的链表#
合并两个排序的链表
https://www.nowcoder.com/practice/d8b6b4358f774294a89de2a6ac4d9337
class Solution: def Merge(self , pHead1: ListNode, pHead2: ListNode) -> ListNode: # write code here head = ListNode(None) rear = head while pHead1 and pHead2: if pHead1.val < pHead2.val: temp = pHead1.next rear.next = pHead1 rear = pHead1 pHead1 = temp else: temp = pHead2.next rear.next = pHead2 rear = pHead2 pHead2 = temp while pHead1: temp = pHead1.next rear.next = pHead1 rear = pHead1 pHead1 = temp while pHead2: temp = pHead2.next rear.next = pHead2 rear = pHead2 pHead2 = temp return head.next