删除链表中重复的元素

在这里插入图片描述

  • /方法一:借助set容器遍历链表筛选,再重新建立链表/
#include<iostream>
#include <vector>
#include <set>
using namespace std;

struct Node {
	Node* next;
	int data;
	Node(int x) :data(x), next(NULL) {}
};

void PrintList(Node* head)//打印链表
{
	Node* phead = head;
	while (phead != nullptr)
	{
		if (phead->next == nullptr)
			cout << phead->data << endl;
		else
			cout << phead->data<<"->";
		phead = phead->next;
	}
}
void PrintSet(set<int>& st)//打印集合
{
	for (auto it : st)
	{
		cout << it<<" ";
	}
	cout << endl;
}
void PrintArr(vector<int>& vec)//打印数组
{
	for (auto aa : vec)
	{
		cout << aa << " ";
	}
	cout << endl;
}
set<int> ArrToSet(vector<int>& vec)//数组转集合
{
	set<int>st;
	for (int i = 0; i < vec.size(); i++)
	{
		st.insert(vec[i]);
	}
	return st;
}
Node* SetToList(set<int>& st)//集合转链表
{
	Node* head=new Node(-1);
	Node* phead = head;
	set<int>::iterator it;
	for (it = st.begin(); it != st.end(); it++)
	{
		Node* NewNode = new Node(*it);
		phead->next = NewNode;
		phead = phead->next;
	}
	return head->next;
}
Node* ArrToList(vector<int>& vec)//数组转链表
{
	Node* head=new Node(vec[0]);
	Node* phead = head;
	for (int i = 1; i < vec.size(); i++)
	{
		Node* NewNode = new Node(vec[i]);
		phead->next = NewNode;
		phead = phead->next;
	}
	return head;
}
vector<int> ListToArr(Node* head)//链表转数组
{
	vector<int> res;
	Node* phead = head;
	while (phead != nullptr)
	{
		res.push_back(phead->data);
		phead = phead->next;
	}
	return res;
}
set<int> Del_Dup_Value(vector<int> &vec)//去除重复元素
{
	set<int> st;
	for (auto ii : vec)
	{
		st.insert(ii);
	}
	return st;
}
int main()
{
	vector<int> vec = {1,1,2,3,3};
	PrintArr(vec);//打印数组
	Node* head1 = ArrToList(vec);
	PrintList(head1);//打印链表1
	set<int> st = Del_Dup_Value(vec);
	PrintSet(st);//打印集合
	Node* head2 = SetToList(st);
	PrintList(head2);//打印链表2
	return 0;
}
  • /方法二:原链表进行更改(遍历相等指向下一个元素继续比,不相等直接不改变)/
#include<iostream>
#include <vector>
#include <set>
using namespace std;

struct Node {
	Node* next;
	int data;
	Node(int x) :data(x), next(NULL) {}
};

void PrintList(Node* head)//打印链表
{
	Node* phead = head;
	while (phead != nullptr)
	{
		if (phead->next == nullptr)
			cout << phead->data << endl;
		else
			cout << phead->data << "->";
		phead = phead->next;
	}
}
void PrintArr(vector<int>& vec)//打印数组
{
	for (auto aa : vec)
	{
		cout << aa << " ";
	}
	cout << endl;
}
Node* ArrToList(vector<int>& vec)//数组转链表
{
	Node* head = new Node(vec[0]);
	Node* phead = head;
	for (int i = 1; i < vec.size(); i++)
	{
		Node* NewNode = new Node(vec[i]);
		phead->next = NewNode;
		phead = phead->next;
	}
	return head;
}
vector<int> ListToArr(Node* head)//链表转数组
{
	vector<int> res;
	Node* phead = head;
	while (phead != nullptr)
	{
		res.push_back(phead->data);
		phead = phead->next;
	}
	return res;
}
Node* Del_Dup_Value(Node* head)//去除重复元素
{
	Node* phead = head;
	while ( phead->next != nullptr) {
		if (phead->data == phead->next->data) {
			phead->next = phead->next->next;
		}
		else {
			phead = phead->next;
		}
	}
	return head;
}
int main()
{
	vector<int> vec = { 1,1,1,2,2,3,3,3};
	PrintArr(vec);//打印数组
	Node* head1 = ArrToList(vec);
	PrintList(head1);//打印链表1
	Node* head2 = Del_Dup_Value(head1);
	PrintList(head2);//打印链表2
	return 0;
}
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

栋哥爱做饭

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

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

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

打赏作者

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

抵扣说明:

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

余额充值