82. 删除排序链表中的重复元素 II(C++题解含VS可运行源程序)

本文介绍了一种使用快慢指针的方法来解决力扣(LeetCode)中删除排序链表中的重复元素II的问题。文章提供了详细的C++代码实现,并附带了在Visual Studio环境下可运行的源程序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

82. 删除排序链表中的重复元素 II(C++题解含VS可运行源程序)

1.题解

快慢指针

  • 关键要保存要删除结点的pre

2.力扣C++源码

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
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->next;
                pre->next = slow;
                if (slow != NULL) fast = slow->next;
            }
            else {
                pre->next = slow;
                pre = pre->next;
                slow = slow->next;
                fast = fast->next;
            }
        }
        return ans_head->next;
    }
};

3.VS可运行源程序

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<limits>
#include<algorithm>
#include<math.h>
#pragma warning(disable:4996)
using namespace std;

//自己独立写出来的,哈哈哈哈哈哈哈哈哈
//自己独立做出来的第一个题
struct ListNode {
	int val;
	ListNode* next;
	ListNode() : val(0), next(nullptr) {}
	ListNode(int x) : val(x), next(nullptr) {}
	ListNode(int x, ListNode* next) : val(x), next(next) {}
};
class Solution {
public:
	ListNode* deleteDuplicates(ListNode* head) {
        //快慢指针   关键要保存要删除结点的pre
        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;
                //若重复,fast就后移,直到第一次出现当前fast与下一个元素不重复的情况
                while ((fast->next != NULL) && (fast->val == fast->next->val)){
                    fast = fast->next;
                }
                slow = fast->next;
                pre->next = slow;//删除重复节点
                if (slow != NULL) fast = slow->next;//先判断slow变换后是否为空
            }
            else {
                pre->next = slow;//移动pre指针
                pre = pre->next;
                slow = slow->next;
                fast = fast->next;
            }
        }
        return ans_head->next;
	}
};
void List_TailInsert2(ListNode** head)//尾插法建立带有头节点的链表
{
    *head = (ListNode*)malloc(sizeof(ListNode));//头节点,不存数据
    ListNode* rear = *head;
    ListNode* p;
    int num;
    scanf("%d", &num);
    while (num != 9999) {//输入9999表示结束
        p = (ListNode*)malloc(sizeof(ListNode));
        p->val = num;
        rear->next = p;
        rear = p;
        scanf("%d", &num);
    }
    rear->next = NULL;
}
int main()
{
    printf("输入链表数据(以9999结束):");
    ListNode* head;
    List_TailInsert2(&head);//传头指针的指针,这样不用返回值了就
    Solution test;
    head = test.deleteDuplicates(head->next);
    printf("删除排序链表中的重复元素后的链表数据为:");
    while (head) {
        printf("%d ", head->val);
        head = head->next;
    }

	printf("\n");
	system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卜凡.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值