当前位置:网站首页>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
}
};
边栏推荐
- 2022年NPDP考完多久出成绩?怎么查询?
- 2022-08-01 mysql/stoonedb慢SQL-Q18分析
- Speed up your programs with bitwise operations
- The state status is displayed incorrectly after the openGauss switch
- Safety (1)
- LeetCode刷题日记: 33、搜索旋转排序数组
- CodeTon Round 2 D. Magical Array 规律
- nacos startup error, the database has been configured, stand-alone startup
- "NetEase Internship" Weekly Diary (3)
- Unable to log in to the Westward Journey
猜你喜欢
From 2023 onwards, these regions will be able to obtain a certificate with a score lower than 45 in the soft examination.
The first time I wrote a programming interview question for Niu Ke: input a string and return the letter with the most occurrences of the string
Remember a pit for gorm initialization
Talking about the "horizontal, vertical and vertical" development trend of domestic ERP
Nanoprobes多组氨酸 (His-) 标签标记:重组蛋白检测方案
Electronic Manufacturing Warehouse Barcode Management System Solution
一次SQL优化,数据库查询速度提升 60 倍
个人博客系统项目测试
LeetCode brush diary: LCP 03. Machine's adventure
[LeetCode Daily Question] - 103. Zigzag Level Order Traversal of Binary Tree
随机推荐
bool框架::PosInGrid (const简历:关键点kp, int &posX, int诗句)
Garbage Collector CMS and G1
一次SQL优化,数据库查询速度提升 60 倍
LeetCode brushing diary: 53, the largest sub-array and
Centos7 install postgresql and enable remote access
Personal blog system project test
BI-SQL丨WHILE
From 2023 onwards, these regions will be able to obtain a certificate with a score lower than 45 in the soft examination.
"NetEase Internship" Weekly Diary (2)
2022-08-01 mysql/stoonedb slow SQL-Q18 analysis
Pinduoduo leverages the consumer expo to promote the upgrading of domestic agricultural products brands and keep pace with international high-quality agricultural products
Chengdu openGauss user group recruit!
"NetEase Internship" Weekly Diary (1)
Can Youxuan database import wrongly be restored?
2022-08-01 Reflection
Power button 1374. Generate each character string is an odd number
NIO‘s Sword(牛客多校赛)
工程师如何对待开源
Remember a gorm transaction and debug to solve mysql deadlock
2022-08-01 安装mysql监控工具phhMyAdmin