forked from Dengshunge/Target-Offer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest18_删除链表的节点.py
125 lines (109 loc) · 3.19 KB
/
test18_删除链表的节点.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# 面试题18 删除链表的节点
'''
题目1的关键在于如何在O(1)的时间内完成删除一个结点,即将后面结点的值赋予给该节点,然后删除后面的那个节点。
题目2是删除重复节点,难点在于想到各种边界条件。
'''
class Node:
#单链表节点
def __init__(self,data,p=None):
self.data = data
self.next = p
def __del__(self):
self.data = None
self.next = None
class Link_List:
def __init__(self):
self.head = Node(None)
def create(self,data):
if len(data) == 0:
print('list is null')
return
self.head.next = Node(data[0])
p = self.head.next
for i in data[1:]:
p.next = Node(i)
p = p.next
def print(self):
p = self.head.next
while p != None:
print(p.data,end=' ')
p = p.next
print('')
#题目一 在O(1)时间内删除链表节点
def DeleteNode(LinkList,pToBeDeleted):
if not isinstance(LinkList,Link_List) or not isinstance(pToBeDeleted,Node):
return
if pToBeDeleted.next != None:
# 若删除的不是尾节点
temp = pToBeDeleted.next
pToBeDeleted.data = temp.data
pToBeDeleted.next = temp.next
elif LinkList.head.next == pToBeDeleted:
# 若链表只有一个结点,删除头结点(也就是尾节点)
LinkList.head.next = None
else:
# 若链表很多节点,删除尾节点
p = LinkList.head.next
while p.next != pToBeDeleted:
p = p.next
p.next = None
# 题目二 删除链表中重复的节点
def DeleteDuplication(LinkList):
if not isinstance(LinkList,Link_List):
return
pPreNode , pNode = LinkList.head.next,LinkList.head.next # pNode指向第一个节点
while pNode.next != None:
if pNode.data != pNode.next.data:
pPreNode = pNode
pNode = pNode.next
else:
# 当数据重复了
temp = pNode.data
while pNode and pNode.data == temp:# 循环到不重复的值或者pNode为None
pNode = pNode.next
if pNode == None:# 当pNode指向空
# 若数据是[1,1]这种头部重复的话,则直接令头指针指向空
if pPreNode == None:
LinkList.head.next = None
return
# 若数据是[1,2,2]这种尾部重复的话,则让pPreNode.next指向空
else:
pPreNode.next = pNode
return
# 尾部重复
pPreNode.next = pNode
# 题目1
# node1 = Node(10)
# node2 = Node(11)
# node3 = Node(13)
# node4 = Node(15)
# node1.next = node2
# node2.next = node3
# node3.next = node4
#
# node5 = Node(17)
#
# S = Link_List()
# S.head.next = node1
# S.print()
# DeleteNode(S,node1)
# S.print()
# 题目2
node1 = Node(1)
node2 = Node(1)
node3 = Node(3)
node4 = Node(3)
node5 = Node(4)
node6 = Node(5)
node7 = Node(5)
node1.next = node2
node2.next = node3
node3.next = node4
node4.next = node5
node5.next = node6
node6.next = node7
S = Link_List()
S.head.next = node1
S.print()
DeleteDuplication(S)
S.print()