LeetCode - Merge Two Sorted Lists

来源:互联网 发布:形婚软件 编辑:程序博客网 时间:2024/06/03 16:29

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 = Noneclass 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
0 0