题解 | #删除有序链表中重复的元素-II#
删除有序链表中重复的元素-II
https://www.nowcoder.com/practice/71cef9f8b5564579bf7ed93fbe0b2024
#define INVALID (0xffffffff)
struct ListNode* deleteDuplicates(struct ListNode* head ) {
struct ListNode* cur = NULL, *next = NULL, *pre = NULL;
struct ListNode tmp = {INVALID, NULL};
struct ListNode* p_tmp = &tmp;
int val = INVALID;
if (head == NULL || head->next == NULL) {
return head;
}
pre = p_tmp;
cur = head;
next = cur->next;
while (cur) {
if (pre->val != cur->val
&& (next == NULL || cur->val != next->val)) {
p_tmp->next = cur;
p_tmp = p_tmp->next;
}
pre = cur;
cur = cur->next;
if (cur) {
next = cur->next;
}
}
p_tmp->next = NULL;
return tmp.next;
}
struct ListNode* deleteDuplicates(struct ListNode* head ) {
struct ListNode* cur = NULL, *next = NULL, *pre = NULL;
struct ListNode tmp = {INVALID, NULL};
struct ListNode* p_tmp = &tmp;
int val = INVALID;
if (head == NULL || head->next == NULL) {
return head;
}
pre = p_tmp;
cur = head;
next = cur->next;
while (cur) {
if (pre->val != cur->val
&& (next == NULL || cur->val != next->val)) {
p_tmp->next = cur;
p_tmp = p_tmp->next;
}
pre = cur;
cur = cur->next;
if (cur) {
next = cur->next;
}
}
p_tmp->next = NULL;
return tmp.next;
}