合并两个排序的链表
合并两个排序的链表_牛客网
https://www.nowcoder.com/practice/d8b6b4358f774294a89de2a6ac4d9337?tpId=13&tqId=11169&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
题目描述
输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。
非递归方法:
def Merge(self, l1, l2): dummy = cur = ListNode(0) while l1 and l2: if l1.val < l2.val: cur.next = l1 l1 = l1.next else: cur.next = l2 l2 = l2.next cur = cur.next cur.next = l1 or l2 return dummy.next
递归方法:
def Merge(self, l1, l2): if not l1 or not l2: return l1 or l2 if l1.val < l2.val: l1.next = self.Merge(l1.next, l2) return l1 else: l2.next = self.Merge(l1, l2.next) return l2
递归方法优化版:
def Merge(self, pHead1, pHead2): # write code here if pHead1 and pHead2: if pHead1.val > pHead2.val: pHead1, pHead2 = pHead2, pHead1 pHead1.next = self.Merge(pHead1.next, pHead2) return pHead1 or pHead2