合并两个排序的链表
Last updated
Last updated
问题简述
合并两个有序链表,且合并后依然有序;
输入两个递增排序的链表,合并这两个链表并使新链表中的节点仍然是递增排序的。
示例1:
输入:1->2->4, 1->3->4
输出:1->1->2->3->4->4
限制:
0 <= 链表长度 <= 1000
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/he-bing-liang-ge-pai-xu-de-lian-biao-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路1:递归
递归公式:merge(l1, l2) = li + merge(li.next, lj)
,
其中当 l1<l2
时 i,j = 1,2
,否则 i,j=2,1
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
def dfs(p1, p2):
if not p1: return p2
if not p2: return p1
if p1.val < p2.val:
p1.next = dfs(p1.next, p2)
return p1
else:
p2.next = dfs(p1, p2.next)
return p2
return dfs(l1, l2)
思路2:迭代
class Solution:
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
ret = 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 if l1 else l2
return ret.next
class Solution:
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
if not l1: return l2
if not l2: return l1
cur = ret = l1 if l1.val < l2.val else l2 #
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 if l1 else l2
return ret