当前位置:网站首页>29. 删除链表中重复的节点
29. 删除链表中重复的节点
2022-08-02 02:20:00 【Hunter_Kevin】
题目
在一个排序的链表中,存在重复的节点,请删除该链表中重复的节点,重复的节点不保留。
数据范围
链表中节点 val 值取值范围 [0,100]。
链表长度 [0,100]。
样例1
输入:1->2->3->3->4->4->5
输出:1->2->5
样例2
输入:1->1->1->2->3
输出:2->3
代码
- 因为头节点都可能被删掉,所以创建一个虚拟头节点指向head,过程中就不用处理第一次插入节点时的判断操作,最终返回虚拟节点的next即可
- 双指针遍历链表
/** * 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) {
if(!head || !head->next)return head;//如果链表为空或只有一个节点,则之间return head
ListNode * pre = head, *cur = head, *ans = new ListNode(-1);//虚拟节点
ListNode * tail = ans; //操作链表的尾指针
while(cur){
//当没有遍历到NULL时,一直遍历链表
int cnt = 0;//统计pre和cur节点中有多少个重复的节点
//当cur不为空并且前后指针的值相同时,一直移动cur指针并且cnt++
while(cur && pre->val == cur->val)cur = cur->next, cnt++;
if(cnt <= 1){
//如果没有pre跟cur之间的节点值没有重复
tail->next = pre;//把pre节点插入到tail后面
tail = tail->next;//更新tail尾指针
pre = cur;//把pre重新置于跟cur同一个位置
}else{
//如果有重复
if(!cur)tail->next = NULL;//当此时是在链表末尾出现重复,则更新tail->next为NULL,下次再进入循环时cur为空会结束循环
pre = cur;//把pre重新置于跟cur同一个位置
}
}
return ans->next;//返回虚拟节点的next
}
};
边栏推荐
- Golang分布式应用之Redis
- 优炫数据库导库导错了能恢复吗?
- EFCore 反向工程
- 考完PMP学什么?前方软考等着你~
- MySQL - CRUD operations
- 字符串常用方法
- PHP live source code to achieve simple barrage effect related code
- From 2023 onwards, these regions will be able to obtain a certificate with a score lower than 45 in the soft examination.
- 2022-08-01 反思
- The failure to create a role in Dahua Westward Journey has been solved
猜你喜欢

Good News | AR opens a new model for the textile industry, and ALVA Systems wins another award!

MySQL optimization strategy

Remember a pit for gorm initialization

LeetCode Brushing Diary: 74. Searching 2D Matrix

Garbage Collector CMS and G1

Nanoprobes免疫测定丨FluoroNanogold试剂免疫染色方案
![[LeetCode Daily Question]——654. The largest binary tree](/img/05/0af1c6dc0085e253c0758c8da63e52.png)
[LeetCode Daily Question]——654. The largest binary tree

MySQL8 download, start, configure, verify

Fundamentals of Cryptography: X.690 and Corresponding BER CER DER Encodings

BioVendor人俱乐部细胞蛋白(CC16)Elisa试剂盒研究领域
随机推荐
Centos7 安装postgresql并开启远程访问
swift project, sqlcipher3 -> 4, cannot open legacy database is there a way to fix it
工程师如何对待开源
【LeetCode每日一题】——103.二叉树的锯齿形层序遍历
Nanoprobes丨1-巯基-(三甘醇)甲醚功能化金纳米颗粒
Use baidu EasyDL implement factory workers smoking behavior recognition
oracle查询扫描全表和走索引
bool框架::PosInGrid (const简历:关键点kp, int &posX, int诗句)
"NetEase Internship" Weekly Diary (3)
Speed up your programs with bitwise operations
FOFAHUB使用测试
2022 Henan Youth Training League Game (3)
接口测试神器Apifox究竟有多香?
C language inserted into the characters of simple exercises
Software testing Interface automation testing Pytest framework encapsulates requests library Encapsulates unified request and multiple base path processing Interface association encapsulation Test cas
openGauss切换后state状态显示不对
MySQL - CRUD operations
60 Feature Engineering Operations: Using Custom Aggregate Functions【Favorites】
LeetCode Review Diary: 153. Find the Minimum Value in a Rotated Sort Array
软件测试 接口自动化测试 pytest框架封装 requests库 封装统一请求和多个基础路径处理 接口关联封装 测试用例写在yaml文件中 数据热加载(动态参数) 断言