题解 | #删除有序链表中重复的元素-II#
删除有序链表中重复的元素-II
https://www.nowcoder.com/practice/71cef9f8b5564579bf7ed93fbe0b2024
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) : val(x), next(nullptr) {}
* };
*/
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
ListNode* my_head = new ListNode(0);
ListNode* cur = my_head;
for(ListNode* left=head;left!=nullptr;)
{
ListNode* right=left;
while(right && left->val == right->val)
{
right = right->next;
}
if(right == left->next)
{
cur->next = left;
cur = left;
cur->next = nullptr;
}
left = right;
}
return my_head->next;
}
};



查看23道真题和解析