# 合并两个有序链表

将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 

 

示例 1:

输入:l1 = [1,2,4], l2 = [1,3,4]
输出:
[1,1,2,3,4,4]

示例 2:

输入:l1 = [], l2 = []
输出:
[]

示例 3:

输入:l1 = [], l2 = [0]
输出:
[0]

 

提示:

## template ```python class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next class LinkList: def __init__(self): self.head=None def initList(self, data): self.head = ListNode(data[0]) r=self.head p = self.head for i in data[1:]: node = ListNode(i) p.next = node p = p.next return r def convert_list(self,head): ret = [] if head == None: return node = head while node != None: ret.append(node.val) node = node.next return ret class Solution: def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode: h = ListNode(0, None) p = h while l1 and l2: if l1.val < l2.val: p.next = l1 p = l1 l1 = l1.next else: p.next = l2 p = l2 l2 = l2.next if l1: p.next = l1 else: p.next = l2 return h.next # %% l = LinkList() list1 = [1,2,4] list2 = [1,3,4] l1 = l.initList(list1) l2 = l.initList(list2) s = Solution() print(l.convert_list(s.mergeTwoLists(l1, l2))) ``` ## 答案 ```python ``` ## 选项 ### A ```python ``` ### B ```python ``` ### C ```python ```