题目
合并两个排好续的链表
分析
略
C++代码实现
1 |
|
Python代码实现
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def (self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
res = ListNode(0)
tmp = res
while l1 is not None and l2 is not None:
if l1.val < l2.val:
tmp.next = ListNode(l1.val)
l1 = l1.next
tmp = tmp.next
else:
tmp.next = ListNode(l2.val)
l2 = l2.next
tmp = tmp.next
if l1 is None:
tmp.next = l2
elif l2 is None:
tmp.next = l1
return res.next
近期评论