递归删除链表中的重复项

杰克·孔

我正在尝试编写一个递归函数来删除链表中的所有相邻重复项。但是,我的功能无法正常工作。

这是我的尝试:

class Node:
    """Node in a linked list"""

    def __init__(self: 'Node', value: object = None, next: 'Node' = None) -> None:
        """Create Node self with data value and successor next."""
        self.value, self.next = value, next

def remove_dup(lnk):
    if not lnk or lnk.next:
        return lnk
    if lnk.value == lnk.next.value:
        lnk.next = lnk.next.next
    else:
        return remove_dup(lnk.next)
特里科特

not优先于or,因此您需要重复not,或将or表达式and放在括号中:

if not (lnk and lnk.next):

此外,当您发现重复项时,不应停止该过程,因为该重复项可能会出现两次以上,并且可能还有其他重复项。

所以它看起来像这样(带有一个将列表表示为字符串的函数):

class Node:
    """Node in a linked list"""

    def __init__(self: 'Node', value: object = None, next: 'Node' = None) -> None:
        """Create Node self with data value and successor next."""
        self.value, self.next = value, next

    def __repr__(self):
        return str(self.value) + ('->' + str(self.next) if self.next else '')

def remove_dup(lnk):
    if not (lnk and lnk.next):
        return lnk
    if lnk.value == lnk.next.value:
        lnk.next = lnk.next.next
        return remove_dup(lnk) # there might be more duplicates...
    else:
        return remove_dup(lnk.next)       

# Sample data and test
lst = Node(1, Node(3, Node(3, Node(3, Node(4, Node(4, Node(5)))))))
print (lst) # 1->3->3-3->4->4->5
remove_dup(lst)
print (lst) # 1->3->4->5

看到它在repl.it 上运行

其他一些改进

该函数remove_dup非常适合作为 的方法Node

如果您可以将链接列表作为标准列表进行读写,那就太好了。类方法可用于从值列表创建链接列表,而该__iter__方法可使您的类可迭代。

然后它可能看起来像这样:

class Node:
    """Node in a linked list"""

    def __init__(self: 'Node', value: object = None, next: 'Node' = None) -> None:
        """Create Node self with data value and successor next."""
        self.value, self.next = value, next

    def __iter__(self):
        yield self.value
        if self.next:
            yield from iter(self.next) # this needs Python 3.4+

    def __repr__(self):
        return '->'.join(map(str, list(self))) # uses __iter__

    def remove_dup(self):
        if not self.next:
            return self
        if self.value == self.next.value:
            self.next = self.next.next
            return self.remove_dup() # there might be more duplicates...
        else:
            return self.next.remove_dup()

    @classmethod
    def from_list(cls, values):
        head = None
        for value in reversed(values):
            head = cls(value, head)
        return head

lst = Node.from_list([1, 3, 3, 3, 4, 4, 5])
print (lst) # 1->3->3->3->4->4->5
lst.remove_dup()
print (lst) # 1->3->4->5
print(list(lst)) # [1, 3, 4, 5]

复制

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章