合并两个链接的排序列表,但得到意外的答案

时间:2019-03-27 08:12:20

标签: python

我为merge two sorted list

写了这样的解决方案
  

合并两个排序的链接列表,并将其作为新列表返回。应该通过将前两个列表的节点拼接在一起来创建新列表。

     

示例:

Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4

我的解决方案:

# Definition for singly-linked list.
class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None
class Solution3:
    def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
        """
        Plan:
        Compare l1 and l2 and merge the remainders
        """
        head = ListNode(0) #create head to hold 
        l3 = ListNode(0)        
        head.next = l3 

        while l1 and l2: #assert both exist
            if l2.val < l1.val:
                l3 = l2 #build l3's node
                l2 = l2.next #this is i++ 
            else:
                l3 = l1
                l1 = l1.next     
            l3 = l3.next #find the next to build
        if l1:
            l3 = l1 
        if l2:
            l3 = l2

        return head.next

但答案错误

  

输入

     

[1,2,4]   [1,3,4]

     

输出

     

[0]

     

预期

     

[1,1,2,3,4,4]

我检查了一下,但逻辑上找不到任何问题。

你能帮我吗?

5 个答案:

答案 0 :(得分:0)

这是我的解决方法

class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None

    def insert(self,el):
        Node = self
        while Node:
            if Node.next==None:
                Node.next = ListNode(el)
                break
            else:
                Node =Node.next

    def prin(node):
        while node:
            if node.next:
                print(node.val,end='-> ')
            else:
                print(node.val)
            node = node.next



def mergeTwoLists(l1: ListNode, l2: ListNode) -> ListNode:
    """
    Plan:
    Compare l1 and l2 and merge the remainders
    """
    _node = None
    if l1.val<l2.val:
        _node = ListNode(l1.val)
        l1 =l1.next
    else:
        _node = ListNode(l2.val)
        l2=l2.next

    l3 = _node        

    while l1 and l2: #assert both exist
        if l2.val < l1.val:
            l3.insert(l2.val)
            l2 = l2.next
        else:
            l3.insert(l1.val)
            l1 = l1.next     

    while l1:
        l3.insert(l1.val)
        l1 =l1.next

    while l2:
        l3.insert(l2.val)
        l2 = l2.next

    return l3


node1= ListNode(1)
node1.insert(2)
node1.insert(4)

node2 = ListNode(1)
node2.insert(3)
node2.insert(4)

solved_list = mergeTwoLists(node1,node2)
solved_list.prin()

答案 1 :(得分:0)

您的函数返回head.next,仅在以下几行中对其进行修改:

l3 = ListNode(0) # this is what's returned
head.next = l3
... # rest of the code changes l3, but never touches head.next
return head.next

您似乎假设使用此设置,对l3的任何分配都会修改head.next,这是不正确的。就目前而言,该函数将始终返回内容为0的新创建节点。

更正确的方法是:

# at the beginning
l3 = head # l3.next is now actually equivalent to head.next
...
# to add a node
l3.next = newnode
l3 = l3.next # moving the pointer to the newly added tail

答案 2 :(得分:0)

您的l3应该标识应该附加下一个节点的位置,因此

   l3 = head

您需要将给定列表(l1l2)中的一个列表的标题附加到l3,但是不需要。
在添加节点之后,您需要同时推进源列表的头指针(l1l2)和目标列表的尾指针(l3):

    while l1 and l2:     #assert both exist
        if l2.val < l1.val:
            l3.next = l2 #append new node to the resulting list
            l2 = l2.next #and skip it in the source list 
        else:
            l3.next = l1
            l1 = l1.next     
        l3 = l3.next     #find the next to build

答案 3 :(得分:0)

您永远不会更改列表结构(循环中没有任何next的分配),您只是在移动l1l2和{{1 }}沿输入列表。
然后返回l3,它仍然是您首先为其分配的节点。

由于使用哨兵节点的方法使代码比没有代码要简单得多,因此我将其保存在这里:

head.next

答案 4 :(得分:0)

快速解决方案:

class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None


class Solution3:
    def merge_two_lists(self, l1: ListNode, l2: ListNode):

        head = ListNode(None)
        l3 = ListNode(None)
        head.next = l3

        while l1 and l2:
            if l2.val < l1.val:
                l3.next = l2
                l2 = l2.next
            else:
                l3.next = l1
                l1 = l1.next
            l3 = l3.next
        if l1:
            l3.next = l1
        if l2:
            l3.next = l2

        head = head.next
        return head.next


head_0, head_0.next, head_0.next.next = ListNode(1), ListNode(2), ListNode(4)
head_1, head_1.next, head_1.next.next = ListNode(1), ListNode(3), ListNode(4)


no = Solution3().merge_two_lists(head_0, head_1)
while no:
    print("Singly-linked list: {}".format(no.val))
    no = no.next

输出:

Singly-linked list: 1
Singly-linked list: 1
Singly-linked list: 2
Singly-linked list: 3
Singly-linked list: 4
Singly-linked list: 4