LeetCode - Merge Two Sorted Lists

LeetCode - Merge Two Sorted Lists

The problem is described as following:

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.

My solution is as following:

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    # @param {ListNode} l1
    # @param {ListNode} l2
    # @return {ListNode}

    def mergeTwoLists(self, l1, l2):
        tmp = ListNode(0)
        current = tmp
        while l1 and l2:
            if l1.val < l2.val:
                current.next = ListNode(l1.val)
                l1 = l1.next
            else:
                current.next = ListNode(l2.val)
                l2 = l2.next
            current = current.next
        if l1:
            current.next = l1
        if l2:
            current.next = l2
        return tmp.next

你可能感兴趣的:(编程题目)