题解 | #删除链表中重复的结点#
删除链表中重复的结点
https://www.nowcoder.com/practice/fc533c45b73a41b0b44ccba763f866ef
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param pHead ListNode类
* @return ListNode类
*/
struct ListNode* deleteDuplication(struct ListNode* pHead ) {
// write code here
if (pHead == NULL || pHead->next == NULL) {
return pHead;
}
struct ListNode* prev = NULL, *cur = pHead, *next = cur->next;
while (next) {
if (cur->val != next->val) {
prev = cur;
cur = next;
next = next->next;
} else {
while (next && cur->val == next->val) {
next = next->next;
}
if (prev) {
prev->next = next;
} else {
pHead = next;
}
//释放
while (cur != next) {
struct ListNode* del = cur;
cur = cur->next;
free(del);
}
if (next) {
next = cur->next;
}
}
}
return pHead;
}

查看15道真题和解析