Python单链表逆置

本文阅读 0 分钟
首页 › Python笔记 › 正文
class Node(object):
    def __init__(self, data=None, next=None):
        self.data = data
        self.next = next

link = Node(1, Node(2, Node(3, Node(4, Node(5, Node(6, Node(7, Node(8, Node(9)))))))))

def rev(link):
    pre = link
    cur = link.next
    pre.next = None
    while cur:
        tmp = cur.next
        cur.next = pre
        pre = cur
        cur = tmp
    return pre

root = rev(link)
while root:
    print root.data
    root = root.next
Python求两棵树是否相同
« 上一篇 04-10
2020二建电子文档[含教材\笔记]
下一篇 » 04-13

发表评论

1 条回复
  1. 文章不错非常喜欢