通过递归方式反向双向链表不在python中迭代

Posted

技术标签:

【中文标题】通过递归方式反向双向链表不在python中迭代【英文标题】:reverse doubly linked list by recursive way not iterate in python 【发布时间】:2019-08-09 10:33:42 【问题描述】:

我如何通过递归为双链表编写反向函数。我已经在python中使用递归和重写来引用问题反向双链表,但它让我陷入无限循环,所以我重写了逻辑,但我有点失去了前一个点

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


class DoublyLinkedList:
    def __init__(self, head):
        self.head = head

    def print_list(self):
        cur = self.head
        while cur is not None:
            print(cur.val)
            cur = cur.next

    def reverse(self):
        if self.head is None or self.head.next is None: return self.head
        cur = self.head
        def reverse_node(node):
            if node is None: return
            if node.next is None:
                node.prev = None
                return node
            new_head = reverse_node(node.next)
            new_node = node.next
            tmp = new_node.next
            new_node.prev = tmp
            new_node.next = node
            node.next = None
            return new_head
        self.head = reverse_node(cur)

a = Node(1, prev=None)
b = Node(2, prev=a)
c = Node(3, prev=b)
d = Node(4, prev=c)
a.next = b
b.next = c
c.next = d
dll = DoublyLinkedList(a)
dll.print_list()
dll.reverse()
dll.print_list()

【问题讨论】:

通过将 dll.reverse() 更改为 dll.reverse_recursive() 我似乎可以正常工作。列表向后打印,头部指向带有val=4 的元素。您能否描述一下您的代码所做的更多不需要的内容? 谢谢你反向反向是我的错字。在self.head = head 之后,实际上我认为我只是通过重写反向函数的逻辑来修复我的代码。我会删除帖子 【参考方案1】:

我所做的只是在最后添加一些打印输出,看看在哪里。在我看来,您的代码就像您期望的那样。在reverse() 函数之后,头部似乎清楚地指向d 而不是a

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


class DoublyLinkedList:
    def __init__(self, head):
        self.head = head

    def print_list(self):
        cur = self.head
        while cur is not None:
            print(cur.val)
            cur = cur.next

    def reverse(self):
        if self.head is None or self.head.next is None: return
        cur = self.head

        def reverse_node(node):
            if node is None: return node
            node.next, node.prev = node.prev, node.next
            if node.prev is None: return node
            return reverse_node(node.prev)

        self.head = reverse_node(cur)

a = Node(1, prev=None)
b = Node(2, prev=a)
c = Node(3, prev=b)
d = Node(4, prev=c)
a.next = b
b.next = c
c.next = d
dll = DoublyLinkedList(a)
print("Head: ",dll.head.val)
dll.print_list()
dll.reverse()
print()
print("Head: ",dll.head.val)
dll.print_list()
print("Is the head at a? ",dll.head is a)
print("Is the head at d? ",dll.head is d)

输出:

Head:  1
1
2
3
4

Head:  4
4
3
2
1
Is the head at a?  False
Is the head at d?  True

【讨论】:

酷,谢谢。我只是更新我的原始帖子。所以我的原始代码无论如何都不起作用。【参考方案2】:

我将在这里发布我的重写反向逻辑,它现在似乎有效。免费评论吧

    def reverse(self):
        if self.head is None or self.head.next is None: return
        cur = self.head

        def reverse_node(node):
            if node is None: return node
            node.next, node.prev = node.prev, node.next
            if node.prev is None: return node
            return reverse_node(node.prev)
        self.head = reverse_node(cur)

【讨论】:

以上是关于通过递归方式反向双向链表不在python中迭代的主要内容,如果未能解决你的问题,请参考以下文章

List双向链表容器

二叉树转换成双向链表

python实现双向链表

Python算法练习--把搜索树转成双向链表

双向链表上的 Java 迭代器

[数据结构]双向链表(C语言)