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.
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution(object): def mergeTwoLists(self, a, b): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ if a and b: if a.val > b.val: a, b = b, a a.next = self.mergeTwoLists(a.next, b) return a or b
以上