python实现 单链表的翻转

 1 #!/usr/bin/env python
 2 #coding = utf-8
 3 class Node:
 4     def __init__(self,data=None,next = None):
 5         self.data = data
 6         self.next = next
 7  
 8 def rev(link):
 9     pre = link
10     cur = link.next
11     pre.next = None
12     while cur:
13         temp = cur.next
14         cur.next = pre
15         pre =cur
16         cur = temp
17     return pre
18  
19 if __name__ == '__main__':
20     link = Node(1, Node(2, Node(3, Node(4, Node(5, Node(6, Node(7, Node(8, Node(9)))))))))
21     root = rev(link)
22     while root:
23         print(root.data)
24         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做好了准备

猜你喜欢

转载自www.cnblogs.com/cupleo/p/10634380.html
今日推荐