Problem
合并两个已排序的链接列表并将其作为新列表返回。新列表应该通过拼接前两个列表的节点来完成。
例:
输入: 1-> 2-> 4,1-> 3-> 4
输出: 1-> 1-> 2-> 3-> 4-> 4
Algorithmic thinking
pass
Python 3 solution
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
# dummy
dummy = pos = ListNode(0)
while l1 and l2:
if l1.val <= l2.val:
pos.next = l1
l1 = l1.next
else:
pos.next = l2
l2 = l2.next
pos = pos.next
pos.next = l1 or l2
return dummy.next