zoukankan      html  css  js  c++  java
  • 21. Merge Two Sorted Lists

    https://leetcode.com/problems/merge-two-sorted-lists/#/description

    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.

    Sol 1:

    Always better to solve linked list problems using recursion. 

    Make sure the returned list starts with smaller node of list 1 and list 2.  

    Recursion.

    # Definition for singly-linked list.
    class ListNode(object):
        def __init__(self, x):
            self.val = x
            self.next = None
    
    class Solution(object):
        def mergeTwoLists(self, l1, l2):
            """
            :type l1: ListNode
            :type l2: ListNode
            :rtype: ListNode
            """
            if not l1 or not l2:
                return l1 or l2
            if l1.val < l2.val:
                l1.next = self.mergeTwoLists(l1.next, l2)
                return l1
            else:
                l2.next = self.mergeTwoLists(l1,l2.next)
                return l2

    Note:

    1 The recursive part the the next link of list 1 or 2. 

    2 Return a list. 

    Sol 2:

    Recursion.

    # Definition for singly-linked list.
    class ListNode(object):
        def __init__(self, x):
            self.val = x
            self.next = None
    
    class Solution(object):
        def mergeTwoLists(self, l1, l2):
            """
            :type l1: ListNode
            :type l2: ListNode
            :rtype: ListNode
            """
            # If both lists are non-empty, I first make sure a starts smaller, use its head as result, and merge the remainders behind it. Otherwise, i.e., if one or both are empty, I just return what's there.
            if l1 and l2:
                if l1.val > l2.val:
                    l1,l2 = l2,l1
                l1.next = self.mergeTwoLists(l1.next, l2)
            return l1 or l2
                

    Sol 3:

    iteratively, O(min(m,n) Space, O(1), in-place

    # Definition for singly-linked list.
    class ListNode(object):
        def __init__(self, x):
            self.val = x
            self.next = None
    
    class Solution(object):
        def mergeTwoLists(self, l1, l2):
            """
            :type l1: ListNode
            :type l2: ListNode
            :rtype: ListNode
            """
            # dummy node is the first node of a linked list, it is also called guard node
            dummy = cur = ListNode(0)
            while l1 and l2:
                if l1.val < l2.val:
                    # choose which node to add to cur
                    cur.next = l1
                    # advance l1, create l1
                    l1 = l1.next
                else:
                    cur.next = l2
                    l2 = l2.next
                # after adding l1 or l2 to cur, create cur
                cur = cur.next
            # assign value to cur.next
            cur.next = l1 or l2
            return dummy.next
                

    Sol 4:

    iteratively, O(min(m,n) Space, O(1), in-place

    # Definition for singly-linked list.
    class ListNode(object):
        def __init__(self, x):
            self.val = x
            self.next = None
    
    class Solution(object):
        def mergeTwoLists(self, l1, l2):
            """
            :type l1: ListNode
            :type l2: ListNode
            :rtype: ListNode
            """
            # cur list is based on l1 and use a tmp varible to add l2 to cur list
            if None in (l1, l2):
                # or returns the non-None list
                return l1 or l2
            dummy = cur = ListNode(0)
            dummy.next = l1
            while l1 and l2:
                if l1.val < l2.val:
                    l1 = l1.next
                else:
                    # store the next link of cur before overwrite it
                    nxt = cur.next
                    # overwrite the next link of cur with l2 
                    cur.next = l2
                    # store the next link of l2 before overwrite it
                    tmp = l2.next
                    # overwrite the next link of l2 with the next link of cur. i.e. the next link of l2 is now assigned to the next link of cur
                    l2.next = nxt
                    # advance l2
                    l2 = tmp
                # advance cur
                cur = cur.next
            # assign the value of the next link of cur
            cur.next = l1 or l2
            return dummy.next
                

    For linked list, recursion beats iteration! Yay!

  • 相关阅读:
    数字化工厂仿真系统-易景空间数字孪生工厂
    会议小程序-智能会议助手在会务系统中的优势
    商场室内地图制作-商场导航-智慧商业综合体
    室内定位室内导航到底能带来什么?
    医院导航系统-智慧医院室内导航-院内导航系统
    室内地图制作-首款实时室内绘制室内地图-3D室内地图
    城市综合三维管网管理-城市三维GIS管线系统-易景地图三维管线地图制作平台
    如何制作好看的室内地图-室内电子地图-在线制作室内地图
    jQuery ui中sortable draggable droppable的使用
    综合CSS3 transition、transform、animation写的一个动画导航
  • 原文地址:https://www.cnblogs.com/prmlab/p/6994783.html
Copyright © 2011-2022 走看看