【问题标题】:How to merge linked lists without sorting? - Python如何在不排序的情况下合并链表? - Python
【发布时间】:2021-12-15 14:54:57
【问题描述】:

如何创建一个简单的函数来合并两个链表,使我可以使用“合并(自我,其他)”之类的方法执行以下操作,而且我不需要对合并的列表进行排序 -我想简单地添加合并功能,我已经包含了驱动程序代码来给出一个想法

ls = [2,3,4,5]
ls2 = [42, 17]
ls.merge(ls2) # should change ls to [2,3,4,5,42,17]
ls2.head.data = 24  # should change ls2 to [24,17] and ls to [2,3,4,5,24,17]
    
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

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

    def merge_sorted(self, llist):
    
        p = self.head 
        q = llist.head
        s = None
    
        if not p:
            return q
        if not q:
            return p

        if p and q:
            if p.data <= q.data:
                s = p 
                p = s.next
            else:
                s = q
                q = s.next
            new_head = s 
        while p and q:
            if p.data <= q.data:
                s.next = p 
                s = p 
                p = s.next
            else:
                s.next = q
                s = q
                q = s.next
        if not p:
            s.next = q 
        if not q:
            s.next = p 
        return new_head

llist_1 = LinkedList()
llist_2 = LinkedList()

llist_1.append(1)
llist_1.append(5)
llist_1.append(7)
llist_1.append(9)
llist_1.append(10)

llist_2.append(2)
llist_2.append(3)
llist_2.append(4)
llist_2.append(6)
llist_2.append(8)

llist_1.merge_sorted(llist_2)
llist_1.print_list()

【问题讨论】:

  • 所以您的merge_sorted 与您的问题无关?那你的尝试在哪里?
  • 代码块的前 4 行也没有意义:标准列表没有 merge 方法。你能清理你的代码并展示你的尝试吗?
  • 我实际上正在处理两个问题,一个是我将要整理的合并方法,另一个是:stackoverflow.com/questions/69781184/…
  • 是的,但这对我的 cmets 不适用。
  • 我的错,我没有删除

标签: python list sorting merge linked-list


【解决方案1】:

我猜append 是更适合简单合并的名称。

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

class LinkedList:
    def __init__(self):
        self.head = None
        self.tail = None # If we don't store tail, `append` would be O(n)

    def append(self, other): # `other` is also a `LinkedList`
        if self.head:
            self.tail.next = other.head
            self.tail = other.tail
        else:
            self.head = other.head
            self.tail = other.tail

【讨论】:

  • 使用这个函数给我一个错误:“NoneType'对象没有属性'next'”,我改变了我原来的帖子,看看合并函数,我能做些什么来简化它和您认为没有排序(只是合并)
  • 我很困惑。您是想简单地逐个追加列表还是要执行合并排序?
  • 只需一个接一个地附加一个列表。目前我的函数使用合并排序,但我希望它简单地合并(不按任何顺序),我怎样才能阻止它按顺序执行?另外,为什么 self.tail.next 会给我错误?
【解决方案2】:

为了实现高效的appendmerge 实现,您需要将tail 属性添加到您的链表实现中

我会投票反对print_list 方法,因为打印不应该由这样的类来管理。而是提供一个方法,该方法将给出列表的字符串表示,并让主程序决定是否打印它。

这是如何工作的:

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


class LinkedList:
    def __init__(self):
        self.head = None
        self.tail = None  # <-- add this to have an efficient append/merge method

    def append(self, data):
        node = Node(data)
        if not self.head:  # When this list is empty
            self.head = node
        else:
            self.tail.next = node
        self.tail = node

    def merge(self, llist):
        if not self.head:  # When this list is empty
            self.head = llist.head
        else:
            self.tail.next = llist.head
        self.tail == llist.tail

    def __iter__(self):  # To facilitate any need to iterate through the list
        node = self.head
        while node:
            yield node.data
            node = node.next

    def __str__(self):  # Don't make a print method; instead provide a string
        return "->".join(map(str, self))  # This calls self.__iter__()


llist_1 = LinkedList()
llist_2 = LinkedList()

llist_1.append(1)
llist_1.append(5)
llist_1.append(7)
llist_1.append(9)
llist_1.append(10)

llist_2.append(2)
llist_2.append(3)
llist_2.append(4)
llist_2.append(6)
llist_2.append(8)

llist_1.merge(llist_2)
print(llist_1)  # Only call `print` here

【讨论】:

    猜你喜欢
    • 2016-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-04
    • 1970-01-01
    • 2016-05-24
    • 2012-04-05
    • 1970-01-01
    • 2011-11-21
    相关资源
    最近更新 更多