删除单链表中最大值结点并输入-1结束程序
时间: 2024-06-13 20:06:59 浏览: 11
以下是删除单链表中最大值结点并输入-1结束程序的Python代码实现:
```python
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
def CreateListTail():
head = ListNode(0)
tail = head
while True:
x = int(input())
if x == -1:
break
node = ListNode(x)
tail.next = node
tail = node
return head.next
def DeleteMaxNode(head):
if not head or not head.next:
return head
max_val = float('-inf')
cur = head
while cur:
max_val = max(max_val, cur.val)
cur = cur.next
dummy = ListNode(0)
dummy.next = head
pre, cur = dummy, head
while cur:
if cur.val == max_val:
pre.next = cur.next
break
pre, cur = cur, cur.next
return dummy.next
head = CreateListTail()
new_head = DeleteMaxNode(head)
while new_head:
print(new_head.val)
new_head = new_head.next
```
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)