您的位置:首页 > 其它

Leetcode 21 Merge Two Sorted Lists

2016-04-07 11:35 197 查看
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

class Solution(object):
def mergeTwoLists(self, l1, l2):
p1, p2 = l1, l2
dummy = ListNode(0)
p = dummy
while p1 and p2:
if p1.val <= p2.val:
p.next = p1
p1 = p1.next
else:
p.next = p2
p2 = p2.next
p = p.next
if p1:
p.next = p1
else:
p.next = p2
return dummy.next
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: