[leetcode]Merge Two Sorted Lists

Merge Two Sorted Lists

# 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):

        start = ListNode(0)
        curNode = start
        while l1 is not None and l2 is not None:
            if l1.val < l2.val:
                curNode.next = l1
                l1 = l1.next
            else:
                curNode.next = l2
                l2 = l2.next
            curNode = curNode.next
        if l1 is not None:
            curNode.next = l1
        if l2 is not None:
            curNode.next = l2
        return start.next
全部评论

相关推荐

11-11 22:08
佛山大学 Java
点赞 评论 收藏
分享
少年郎as:这不把公司名贴出来那我可要喷你了哦
点赞 评论 收藏
分享
双尔:你就写拥有ai开发经历,熟练运用提示词,优化ai,提高ai回答质量
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务