在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留。用完整的C语言程序实现
时间: 2024-05-28 07:15:28 浏览: 95
删除单链表中重复结点
4星 · 用户满意度95%
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* deleteDuplication(struct ListNode* head) {
if (head == NULL || head->next == NULL) {
return head;
}
struct ListNode* preNode = NULL;
struct ListNode* curNode = head;
while (curNode) {
struct ListNode* nextNode = curNode->next;
int flag = 0;
while (nextNode && nextNode->val == curNode->val) {
flag = 1;
nextNode = nextNode->next;
}
if (flag == 1) {
if (preNode == NULL) {
head = nextNode;
} else {
preNode->next = nextNode;
}
curNode = nextNode;
} else {
preNode = curNode;
curNode = curNode->next;
}
}
return head;
}
阅读全文