# 定义链表节点类
class ListNode:
    def __init__(self, value=0, next=None):
        self.val = value
        self.next = next

# 创建单链表
head = ListNode()  # 头节点
while True:
    val = input().strip()
    if val == '-1':
        break
    node = ListNode(int(val))
    node.next = head.next
    head.next = node

# 删除最大值
max_val = head.next.val
pre_node = head
cur_node = head.next
while cur_node:
    if cur_node.val > max_val:
        max_val = cur_node.val
        pre_node = pre_node.next
        cur_node = cur_node.next
    else:
        pre_node.next = cur_node.next
        cur_node = pre_node.next

# 输出单链表
cur_node = head.next
while cur_node:
    print(cur_node.val, end=' ')
    cur_node = cur_node.next
Python 单链表删除最大值算法实现

原文地址: https://www.cveoy.top/t/topic/oLBF 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录