question about the test case.


  • 0
    S
    # 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 = cur = ListNode(0)
            while l1 and l2:
                if l1.val < l2.val:
                    cur.next = l1
                    l1 = l1.next
                else:
                    cur.next = l2
                    l2 = l2.next
                cur = cur.next
            cur.next = l1 or l2
            return dummy.next
    

    say I want to merge two linked list 1->2->4, 3->6->10. How do i use the ListNode() to generate the two lists?
    Or how do I run it off leetcode?


  • 0
    G

    @shuai18
    Maybe you can use this:
    l1 = ListNode(1)
    l1.next = ListNode(2)
    l1.next.next = ListNode(4)
    l2 = ListNode(3)
    l2.next = ListNode(6)
    l2.next.next = ListNode(10)


  • 0
    S

    @gaoshuaibo Hi thank you for your reply.
    How do I call the newlist?

    ll=LinkedList()
    ll.mergeTwoLists1(l1,l2)
    

    Like this? It does not run.


  • 0
    G

    @shuai18
    Hi, here are my solutions:
    l1 = ListNode(1)
    l1.next = ListNode(2)
    l1.next.next = ListNode(4)
    l2 = ListNode(2)
    s = Solution()
    re = s.mergeTwoLists(l1,l2)
    while re != None:
    print(re.val)
    re = re.next


Log in to reply
 

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