题解 | #合并两个排序的链表#
合并两个排序的链表
http://www.nowcoder.com/practice/d8b6b4358f774294a89de2a6ac4d9337
1、构造伪节点防止链表为空
2、充分利用递增排序的思想。从头节点开始比较
class Solution: # 返回合并后列表 def Merge(self, pHead1, pHead2): dummy = ListNode(-1) cur = dummy while pHead1 and pHead2: if pHead1.val <= pHead2.val: cur.next = pHead1 pHead1 = pHead1.next else: cur.next = pHead2 pHead2 = pHead2.next cur = cur.next cur.next = pHead1 if pHead1 else pHead2 return dummy.next