首页 > 编程语言 > 详细

python实现单链表的反转

时间:2018-09-15 21:52:53      阅读:146      评论:0      收藏:0      [点我收藏+]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/env python
#coding = utf-8
class Node:
    def __init__(self,data=None,next = None):
        self.data = data
        self.next = next
 
def rev(link):
    pre = link
    cur = link.next
    pre.next = None
    while cur:
        temp = cur.next
        cur.next = pre
        pre =cur
        cur = temp
    return pre
 
if __name__ == ‘__main__‘:
    link = Node(1, Node(2, Node(3, Node(4, Node(5, Node(6, Node(7, Node(8, Node(9)))))))))
    root = rev(link)
    while root:
        print(root.data)
        root =root.next

  解释一下rev函数的实现过程:

line 9-11是将原链表的第一个节点变成了新链表的最后一个节点,同时将原链表的第二个节点保存在cur中

line13-16就是从原链表的第二个节点开始遍历到最后一个节点,将所有节点翻转一遍

以翻转第二个节点为例

temp = cur.next是将cur的下一个节点保存在temp中,也就是第节点3,因为翻转后,节点2的下一个节点变成了节点1,原先节点2和节点3之间的连接断开,通过节点2就找不到节点3了,因此需要保存

cur.next = pre就是将节点2的下一个节点指向了节点1

然后pre向后移动到原先cur的位置,cur也向后移动一个节点,也就是pre = cur ,cur =temp

这就为翻转节点3做好了准备

 

转自:https://www.cnblogs.com/xqn2017/p/8021666.html

python实现单链表的反转

原文:https://www.cnblogs.com/songzhenhua/p/9652292.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!