[TOP202]题解 | #牛群排列去重#
牛群排列去重
https://www.nowcoder.com/practice/8cabda340ac6461984ef9a1ad66915e4
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(nullptr) {}
* };
*/
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
ListNode* current = head;
while (current && current->next) {
if (current->val == current->next->val) {
ListNode* duplicateNode = current->next;
current->next = duplicateNode->next; // 跳过重复节点
delete duplicateNode; // 释放重复节点的内存
} else {
current = current->next; // 继续遍历下一个节点
}
}
return head; // 返回去重后的链表头节点
}
};
华为HUAWEI公司氛围 740人发布