82. 删除排序链表中的重复元素 II(C++题解含VS可运行源程序)
1.题解
快慢指针
2.力扣C++源码
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if (head == NULL || head->next == NULL)return head;
ListNode* pre = (ListNode*)malloc(sizeof(ListNode));
ListNode* slow = head;
ListNode* fast = head->next;
pre->next = slow;
ListNode* ans_head = pre;
while (slow != NULL && fast != NULL) {
if (slow->val == fast->val) {
int num = slow->val;
ListNode* p = fast;
while ((fast->next != NULL) && (fast->val == fast->next->val)){
fast = fast->next;
}
slow = fast