leetcode 21. 合并两个有序链表

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

 1 # Definition for singly-linked list.
 2 # class ListNode:
 3 #     def __init__(self, x):
 4 #         self.val = x
 5 #         self.next = None
 6 
 7 class Solution:
 8     def mergeTwoLists(self, l1, l2):
 9         """
10         :type l1: ListNode
11         :type l2: ListNode
12         :rtype: ListNode
13         """
14         res = []
15         while l1:
16             res.append(l1.val)
17             l1 = l1.next
18             
19         while l2:
20             res.append(l2.val)
21             l2 = l2.next
22             
23         res.sort()
24         
25         l = ListNode(0)
26         p = l
27         for i in res:
28             node = ListNode(i)
29             p.next = node
30             p = p.next
31         return l.next
原文地址:https://www.cnblogs.com/chengchengaqin/p/9511062.html