-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1_1.py
56 lines (50 loc) · 1.2 KB
/
1_1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#单链表数据结构
class LNode:
def __init__(self,x):
self.data = x
self.next = None
#带头结点的链表逆序
def Reverse(head):
#判断链表是否为空
if head.next == None or head.next.next == None:
return
pre = None
cur = None
nex = None
#链表首结点变为尾结点
cur = head.next
nex = cur.next
cur.next = None
pre = cur
cur = nex
#使当前遍历到的结点cur指向其前驱节点
while cur.next != None:
nex = cur.next
cur.next = pre #!!!
pre = cur
cur = nex
#链表最后一个结点指向倒数第二个结点
cur.next = pre
#头结点指向原来链表的尾结点
head.next = cur
if __name__=="__main__":
i = 1
head = LNode(0)
cur = head
#构造单链表
while i<8:
tmp = LNode(i)
cur.next = tmp
cur = tmp
i += 1
print(u'逆序前:',end = " ")
cur = head.next
while cur !=None:
print(cur.data,end = " ")
cur = cur.next
print(u"\n逆序后:",end = " ")
Reverse(head)
cur = head.next
while cur != None:
print(cur.data,end = " ")
cur = cur.next