# 面试题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()
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
python经典练习及面试题之1.zip (16个子文件)
python经典练习及面试题之1
test7_重建二叉树.py 2KB
test14_剪绳子.py 2KB
test15_二进制中1的个数.py 1KB
test13_机器人的运动范围.py 2KB
test5_替换空格.py 2KB
test3_数组中重复的数字.py 3KB
test9_用两个栈实现队列.py 728B
test16_数值的整数次方.py 982B
test4_二维数组中的查找.py 1KB
test17_打印从1到最大的n位数.py 2KB
test8_二叉树的下一个节点.py 2KB
test12_矩阵中的路径.py 2KB
test11_旋转数组的最小数字.py 1KB
test10_菲波那切数列.py 794B
test6_从尾到头打印链表.py 2KB
test18_删除链表的节点.py 3KB
共 16 条
- 1
资源评论
梦回阑珊
- 粉丝: 2439
- 资源: 619
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功