給定兩個有序鏈表,鏈接他們,元素從小到大排列
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
__title__ = ''
__author__ = 'thinkreed'
__mtime__ = '2017/3/23'
"""
class Solution(object):
def mergeTwoLists(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
if not l1:
return l2
if not l2:
return l1
if l1.val < l2.val:
#l1需要去鏈接后續(xù)節(jié)點
l1.next = self.mergeTwoLists(l1.next, l2)
return l1
else:
#l2去鏈接后續(xù)節(jié)點
l2.next = self.mergeTwoLists(l1, l2.next)
return l2