删除单链表中最大值结点并返回值
时间: 2024-06-13 07:06:00 浏览: 190
以下是两种删除单链表中最大值结点并返回值的算法:
1. 元素值各不相同的情况
```python
def DelMax(L):
p = L.next
q = L
maxp = p
maxq = q
max = p.data
while p != None:
if maxp.data < p.data:
maxp = p
maxq = q
q = p
p = p.next
maxq.next = maxp.next
del maxp
return max
```
2. 元素值有相同情况的情况
```python
def DelMax(L):
p = L.next
p1 = p.next
max = p.data
while p1 != None:
if p1.data > p.data:
max = p1.data
p = p.next
p1 = p.next
p = L
p1 = p.next
while p1 != None:
if p1.data == max:
s = p1
p.next = p1.next
del s
else:
p = p.next
if p != None:
p1 = p.next
else:
p1 = None
return max
```
阅读全文