在python中将两个排序的链表合并为一个链表

2024-05-15 22:18:30 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的代码:

def merge_lists(head1, head2):
    if head1 is None and head2 is None:
        return None
    if head1 is None:
        return head2
    if head2 is None:
        return head1
    if head1.value < head2.value:
        temp = head1
    else:
        temp = head2
    while head1 != None and head2 != None:
        if head1.value < head2.value:
            temp.next = head1
            head1 = head1.next
        else:
            temp.next = head2
            head2 = head2.next
    if head1 is None:
        temp.next = head2
    else:
        temp.next = head1
    return temp
    pass

这里的问题在无限循环中受阻了。有人能告诉我问题是什么吗

例如:

 assert [] == merge_lists([],[])
 assert [1,2,3] == merge_lists([1,2,3], [])
 assert [1,2,3] == merge_lists([], [1,2,3])
 assert [1,1,2,2,3,3,4,5] == merge_lists([1,2,3], [1,2,3,4,5])

Tags: and代码nonereturnifisvalueassert
3条回答

当前代码的问题是,在从当前节点导航到下一个节点之前,它会导致临时节点next的副作用。当当前temp节点是当前节点时,这是有问题的。

也就是说,想象一下这个案例:

temp = N
temp.next = N  # which means N.next = N
N = N.next     # but from above N = (N.next = N) -> N = N

有一个已更正的版本,其中包含一些其他更新:

def merge_lists(head1, head2):
    if head1 is None:
        return head2
    if head2 is None:
        return head1

    # create dummy node to avoid additional checks in loop
    s = t = node() 
    while not (head1 is None or head2 is None):
        if head1.value < head2.value:
            # remember current low-node
            c = head1
            # follow ->next
            head1 = head1.next
        else:
            # remember current low-node
            c = head2
            # follow ->next
            head2 = head2.next

        # only mutate the node AFTER we have followed ->next
        t.next = c          
        # and make sure we also advance the temp
        t = t.next

    t.next = head1 or head2

    # return tail of dummy node
    return s.next

完整代码:-

为链表的每个节点定义“Node”类。

class Node:
    def __init__(self,data):
        self.data = data
        self.next = None

“linkedlist”类的定义。

class linkedlist:
    def __init__(self):
        self.head = None

“合并”功能的定义。

参数“ll1”和“ll2”是两个链表的头。

def merge_lists(ll1, ll2):
    if ll1 is None:
        return ll2
    if ll2 is None:
        return ll1

    if (ll1.data < ll2.data):
        ll1.next = merge_lists(ll1.next, ll2)
        return ll1
    else:
        ll2.next = merge_lists(ll2.next, ll1)
        return ll2

在列表中输入。

l1 = []
try:
    l1 = list(map(int,input().strip().split()))
except EOFError:
    pass
l2 = []
try:
    l2 = list(map(int,input().strip().split()))
except EOFError:
    pass

从输入列表值创建链表,即ll1ll2

ll1 = linkedlist()
ll1.head = Node(l1[0])
itr1 = ll1.head
for i in range(1,n1):
    temp = Node(l1[i])
    itr1.next = temp
    itr1 = itr1.next

ll2 = linkedlist()
ll2.head = Node(l2[0])
itr2 = ll2.head
for i in range(1,n2):
    temp = Node(l2[i])
    itr2.next = temp
    itr2 = itr2.next

通过传递两个链表的头,使用merge函数合并两个排序的链表

itr = merge(ll1.head,ll2.head)

“merge”函数返回迭代器本身,迭代器的值打印为:

while itr != None:
    print(itr.data,end=" ")
    itr = itr.next

自定义输入和输出:-

输入

1个

4个

1 3 5 7年

4个

2 4 6 12个

输出

1 2 3 4 5 6 7 12

两个排序链表合并的递归算法

def merge_lists(h1, h2):
    if h1 is None:
        return h2
    if h2 is None:
        return h1

    if (h1.value < h2.value):
        h1.next = merge_lists(h1.next, h2)
        return h1
    else:
        h2.next = merge_lists(h2.next, h1)
        return h2

相关问题 更多 >