Premiumlab  

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!

posted on 2017-06-12 20:11  Premiumlab  阅读(135)  评论(0编辑  收藏  举报