当前位置:网站首页>JZ76 删除链表中重复的结点
JZ76 删除链表中重复的结点
2022-04-23 02:40:00 【Rosita.】
题目链接:删除链表中重复的结点_牛客题霸_牛客网
注意点:
1.考虑头节点重复,所以构造虚拟头节点
1.方法一参考了评论区:申请两个指针的pre和cur,pre指向cur的前一个节点,cur指向当前节点,如果cur和cur->next的val想等,就循环找出所有重复节点的末尾,然后pre跳到不重复的节点上即可,时间和空间复杂度都是O(n)。
2.方法二:哈希去重,记录每个节点出现的次数,大于1的指向下一个节点
目录
方法一:双指针
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
*/
class Solution {
public:
ListNode* deleteDuplication(ListNode* pHead) {
if (!pHead) return NULL;
//创建虚拟头节点,防止第一个节点就重复的问题
ListNode* nhead = new ListNode(0);
nhead->next = pHead;
ListNode* pre= nhead, *cur = pHead;
while(cur){
//相邻节点值相等
if(cur ->next && cur->val == cur->next->val){
//找出所有的相同节点
while(cur->next && cur->val == cur->next->val){
cur = cur->next;
}
cur = cur->next;
//跳过重复节点链接
pre ->next = cur;
}
else{
pre = cur;
cur = cur->next;
}
}
return nhead->next;
}
};
方法二:哈希
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
*/
class Solution {
public:
ListNode* deleteDuplication(ListNode* pHead) {
if (!pHead) return NULL;
//创建虚拟头节点,防止第一个节点就重复的问题
ListNode* nhead = new ListNode(0);
nhead->next = pHead;
ListNode *cur = nhead;
//记录每个节点的个数
unordered_map<int, int> map;
while(cur){
map[cur->val]++;
cur = cur->next;
}
//重新遍历链表,删除重复元素
cur = nhead;
while(cur->next){
if(map[cur->next->val] != 1){
//跳过重复元素
cur->next = cur->next->next;
}else{
cur = cur->next;
}
}
//返回头节点
return nhead->next;
}
};
版权声明
本文为[Rosita.]所创,转载请带上原文链接,感谢
https://blog.csdn.net/pure_dreams/article/details/124350930
边栏推荐
- Renesas electronic MCU RT thread development and Design Competition
- Devil cold rice 𞓜 078 devil answers the market in Shanghai and Nanjing; Communication and guidance; Winning the country and killing and screening; The purpose of making money; Change other people's op
- 学习正则表达式选项、断言
- 006_ redis_ Jedis quick start
- 基于Torchserve部署SBERT模型<语义相似度任务>
- Tp6 Alibaba cloud SMS window reports curl error 60: SSL certificate problem: unable to get local issuer certificate
- After idea is successfully connected to H2 database, there are no sub files
- Want to experience homekit smart home? Why don't you take a look at this smart ecosystem
- The usage and difference of * and & in C language and the meaning of keywords static and volatile
- Understanding process (multithreading primary)
猜你喜欢

全局、獨享、局部路由守衛

Halo open source project learning (I): project launch

一个国产图像分割项目重磅开源!

Arduino esp8266 network upgrade OTA

MySQL JDBC programming

After idea is successfully connected to H2 database, there are no sub files

So library dependency

Efficient music format conversion tool Music Converter Pro

RT_Thread自问自答

Rhcsa day 1 operation
随机推荐
php+mysql对下拉框搜索的内容修改
010_ StringRedisTemplate
Global, exclusive, local Routing Guard
[XJTU計算機網絡安全與管理]第二講 密碼技術
006_ redis_ Jedis quick start
Devil cold rice 𞓜 078 devil answers the market in Shanghai and Nanjing; Communication and guidance; Winning the country and killing and screening; The purpose of making money; Change other people's op
每日一题冲刺大厂第十六天 NOIP普及组 三国游戏
Execute external SQL script in MySQL workbench and report error
Flink stream processing engine system learning (II)
013_ Analysis of SMS verification code login process based on session
高效音乐格式转换工具Music Converter Pro
Rich intelligent auxiliary functions and exposure of Sihao X6 security configuration: it will be pre sold on April 23
在MySQL Workbench中执行外部的SQL脚本,报错
Looking for a job, writing a resume to an interview, this set of information is enough!
Rhcsa day 4 operation
全局、独享、局部路由守卫
A domestic image segmentation project is heavy and open source!
[xjtu Computer Network Security and Management] session 2 Cryptographic Technology
002_ Redis_ Common operation commands of string type
Daily question (April 22, 2022) - rotation function