1. 伪代码
1. 递归版本
Function MergeTwoLists(l1, l2):
If l1 is null:
Return l2
If l2 is null:
Return l1
If l1.value < l2.value:
l1.next = MergeTwoLists(l1.next, l2)
Return l1
Else:
l2.next = MergeTwoLists(l1, l2.next)
Return l2
2. 循环遍历版本
Function MergeTwoLists(l1, l2):
Create a dummy node with arbitrary value
Set current to point to dummy
While l1 is not null and l2 is not null:
If l1.value < l2.value:
current.next = l1
l1 = l1.next
Else:
current.next = l2
l2 = l2.next
current = current.next
If l1 is null:
current.next = l2
Else:
current.next = l1
Return dummy.next