c语音中实验题目:删除单链表中最大值结点并返回值
时间: 2024-06-13 08:07:04 浏览: 94
以下是C语言中删除单链表中最大值结点并返回值的代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct ListNode {
int val;
struct ListNode *next;
} ListNode;
int deleteMax(ListNode **head) {
if (*head == NULL) {
return -1;
}
ListNode *p = *head;
ListNode *pre = NULL;
ListNode *maxNode = *head;
while (p != NULL) {
if (p->val > maxNode->val) {
maxNode = p;
}
p = p->next;
}
p = *head;
while (p != NULL) {
if (p == maxNode) {
if (pre == NULL) {
*head = p->next;
} else {
pre->next = p->next;
}
int val = p->val;
free(p);
return val;
}
pre = p;
p = p->next;
}
return -1;
}
```
阅读全文