题解 | #删除链表中重复的结点#
删除链表中重复的结点
https://www.nowcoder.com/practice/fc533c45b73a41b0b44ccba763f866ef
struct ListNode* deleteDuplication(struct ListNode* pHead ) {
// write code here
if (pHead == NULL)
return NULL;
struct ListNode *head = (struct ListNode*)malloc(sizeof(struct ListNode));
head->next = pHead;
struct ListNode *cur = head;
struct ListNode *fre = NULL;
int val = 0;
while (cur->next->next)
{
if (cur->next->val == cur->next->next->val)
{
val = cur->next->val;
fre = cur->next;
cur->next = cur->next->next;
free(fre);
}
else
{
if (cur->next->val == val)
{
fre = cur->next;
cur->next = cur->next->next;
free(fre);
}
else
cur = cur->next;
}
}
if (cur->next->val == val)
{
fre = cur->next;
cur->next = NULL;
free(fre);
}
cur = head->next;
free(head);
return cur;
}
查看1道真题和解析