删除有序数组重复项I
https://leetcode.cn/problems/remove-duplicates-from-sorted-array/
经典的双指针模板
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int p = 0;
for (int i = 0, j = 0; i < nums.size(); i ++) {
while (j < nums.size() && nums[i] == nums[j]) j++;
nums[p ++] = nums[i];
i = j - 1;
}
return p;
}
};
删除链表重复节点
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* deleteDuplication(ListNode* head) {
auto dummy = new ListNode(-1);
dummy->next = head;
auto p = dummy;
while (p->next)
{
auto q = p->next;
while (q && q->val == p->next->val) q = q->next;
if (p->next->next == q) p = p->next;
else p->next = q;
}
return dummy->next;
}
};