Simple 5 lines Python


  • 26

    Solution 1

    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.

    class Solution:
        def mergeTwoLists(self, a, b):
            if a and b:
                if a.val > b.val:
                    a, b = b, a
                a.next = self.mergeTwoLists(a.next, b)
            return a or b
    

    Solution 2

    First make sure that a is the "better" one (meaning b is None or has larger/equal value). Then merge the remainders behind a.

    def mergeTwoLists(self, a, b):
        if not a or b and a.val > b.val:
            a, b = b, a
        if a:
            a.next = self.mergeTwoLists(a.next, b)
        return a

  • 0
    A

    It looks very elegant!
    It must be a very trivial question - I am just starting off with programming. As I understand, we have that a and b are objects that represent nodes of list. But what is meant by 'a and b' and 'a or b'? Why is the output is the start node of the merged list?

    Thanks.


  • 0

    Quote from the documentation (always a good place to look):

    The expression x and y first evaluates x; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned.

    The expression x or y first evaluates x; if x is true, its value is returned; otherwise, y is evaluated and the resulting value is returned.

    In our case here, note that actual nodes are true and that None is false.


  • 1
    R

    Wow.man,you are the god in programming


  • 0
    M
    This post is deleted!

  • 0
    A

    @StefanPochmann Hi, suppose a list 'A' is 1->3->5 and list 'B' is 4->6->7
    how would you handle merging them with condition that they need to be sorted
    I would like to share my view about this, please let me know if it needs improvement,
    i) Compare first node of 'B' with each node of 'A'
    while A.val<B.val
    A=A.next
    We get 'A' pointing to node whose value is lesser than node of 'B'
    ii) As per the example, intent is to store '4' in '3' 's reference and prior to that store '3' 's reference in a temp

    iii) The same will have to be done for nodes in 'A', as in '5' will have to be stored between 4 and 6

    Please review this and help me in improvising


  • 0

    @ash.ps312 said in Simple 5 lines Python:

    @StefanPochmann Hi, suppose a list 'A' is 1->3->5 and list 'B' is 4->6->7
    how would you handle merging them with condition that they need to be sorted

    Like my solutions do?


  • 0
    D

    OMG,very nice!!


  • 0
    N

    @StefanPochmann said in Simple 5 lines Python:

    might be a silly question. what about the condition of a =b


  • 0
    H
    This post is deleted!

  • 0
    H

    @StefanPochmann what will happen when a.next=null in solution 2?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.