美文网首页
42.单链表逆置

42.单链表逆置

作者: vbuer | 来源:发表于2018-09-04 09:55 被阅读5次
    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
    

    相关文章

      网友评论

          本文标题:42.单链表逆置

          本文链接:https://www.haomeiwen.com/subject/mjzniftx.html