将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
方案一:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def mergeTwoLists(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
if not l1 and not l2:
return
res = []
q1, q2 = l1, l2
while q1:
res.append(q1)
q1 = q1.next
while q2:
res.append(q2)
q2 =q2.next
res = sorted(res, key=lambda x:x.val)
return [i.val for i in res]