83. Remove Duplicates from Sorted List + 82. Remove Duplicates from Sorted List II
▶ 删除单链表中的重复元素。
▶ 83. 把重复元素删得只剩一个,如 1 → 1 → 2 → 3 → 3 → 3 → 4 → 5 → 5 变成 1 → 2 → 3 → 4 → 5。注意要点:第一个元素就可能重复,最后一个元素可能是重复,多个连续重复。
● 自己的代码,18 ms,记录了发生重复的第一个元素的位置,使其指向下一个不同的元素的位置。
class Solution
{
public:
ListNode* deleteDuplicates(ListNode* head)
{
if (head == nullptr || head->next == nullptr)
return head; ListNode *output, *i, *j, *k;
for (i = head, j = head->next; i != nullptr; i = j)
{
for (; j != nullptr && i->val == j->val; j = j->next);
i->next = j;
}
return head;
}
};
● 大佬的代码,11 ms,逐格比较相邻两个结点,值相同则跨过后一结点。
class Solution
{
public:
ListNode* deleteDuplicates(ListNode* head)
{
for (ListNode* cur = head; cur && cur->next;)
{
if (cur->val == cur->next->val)
cur->next = cur->next->next;
else
cur = cur->next;
}
return head;
}
};
▶ 82. 把发生重复的元素删除得一个不剩。如 1 → 1 → 2 → 3 → 3 → 3 → 4 → 5 → 5 变成 2 → 4 。
● 自己的非递归版代码,8 ms
class Solution
{
public:
ListNode* deleteDuplicates(ListNode* head)
{
if (head == nullptr || head->next == nullptr)
return head;
for (; head != nullptr && head->next != nullptr && head->val == head->next->val;)// 去掉开头的重复结点
{
for (; head->next != nullptr && head->val == head->next->val; head = head->next);
head = head->next;
}
if (head == nullptr)
return head;
for (ListNode *prev = head, *cur = head->next; cur != nullptr && cur->next != nullptr; cur = prev->next)
{
if (cur->val == cur->next->val)
{
for (; cur->next != nullptr && cur->val == cur->next->val; cur->next = cur->next->next);
prev->next = cur->next;
}
else
prev = prev->next;
}
return head;
}
};
● 大佬的递归版代码,9 ms
class Solution
{
public:
ListNode* deleteDuplicates(ListNode* head)
{
if (head == nullptr || head->next == nullptr)
return head; int val = head->val;
ListNode *p = head->next;
if (p->val != val)
{
head->next = deleteDuplicates(p);
return head;
}
else
{
for (; p && p->val == val; p = p->next);
return deleteDuplicates(p);
}
}
};
83. Remove Duplicates from Sorted List + 82. Remove Duplicates from Sorted List II的更多相关文章
- leetcode 203. Remove Linked List Elements 、83. Remove Duplicates from Sorted List 、82. Remove Duplicates from Sorted List II(剑指offer57 删除链表中重复的结点)
203题是在链表中删除一个固定的值,83题是在链表中删除重复的数值,但要保留一个:82也是删除重复的数值,但重复的都删除,不保留. 比如[1.2.2.3],83题要求的结果是[1.2.3],82题要求 ...
- 82. Remove Duplicates from Sorted List II && i
题目 83. Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such tha ...
- 82. Remove Duplicates from Sorted List II【Medium】
82. Remove Duplicates from Sorted List II[Medium] Given a sorted linked list, delete all nodes that ...
- Day07_39_集合中的remove()方法 与 迭代器中的remove()方法
集合中的remove()方法 与 迭代器中的remove()方法 深入remove()方法 iterator 中的remove()方法 collection 中的remove(Object)方法 注意 ...
- [LeetCode] 82. Remove Duplicates from Sorted List II_Medium tag: Linked List
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinctnumbe ...
- [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项之二
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项 II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- 【LeetCode】82. Remove Duplicates from Sorted List II 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/remove-du ...
- [LeetCode#82]Remove Duplicates from Sorted Array II
Problem: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? F ...
随机推荐
- ES5和ES6中的继承
看到一篇写的非常好的关于js继承的文章,其中对构造函数.原型.实例之间的关系的描述十分透彻,故转载作者文章以随时学习,并供大家共同进步! ES5 ES5中的继承,看图: function Super( ...
- mysql--------命令来操作表
常用的通过mysql命令来更改表结构的一些sql语句,包括添加.删除.修改字段.调整字段顺序. 添加字段: alter table `user_movement_log` Add column Gat ...
- js做小数运算精度问题
当js做小数运算时存在bug,大概是因为二进制和十进制转换之间的关系. bug如图 解决方案 1.运算结果后,乘以100再除以100.网上推荐这种方法但是乘以1000再除以1000依然存在精度问题 2 ...
- Appium 自动化测试(9) -- 在Uiautomator中查看webview元素
在uiautomator中,直接查看不到webview中的元素,不知道大家遇到过没有?如下 解决方法如下: step0:将uiautomator关闭 step1:在appium 中,设置Android ...
- 同一sql表,在页面展示多级菜单
2.
- TWain 在 Qt4 中的调用方法
1.开发环境 Windows7_sp1_x64 Qt4.7.4 + mingw32 twain_32.dll (1.7.1.3) 2.常用缩写 DSM: Data Source Manager 数据源 ...
- 如何写入和读取从 Microsoft 消息队列在 Visual C#
注意:这篇文章是由无人工介入的微软自动的机器翻译软件翻译完成.微软很高兴能同时提供给您由人工翻译的和由机器翻译的文章, 以使您能使用您的语言访问所有的知识库文章.然而由机器翻译的文章并不总是完美的.它 ...
- String类的concat()方法
String类的concat()方法: public class MyClass { public static void main(String[] args) { String str1=&quo ...
- 免费获取 Kaspersky Small Office Security 90 天授权
Kaspersky Small Office Security 是卡巴斯基出品的企业版杀毒软件,目前美国官网上官方有赠送活动,能够免费获取 90 天的授权,但必须要使用美国代理. 获取地址:http: ...
- SOA实践指南-读书笔记
SOA是英文Service-Oriented Architecture,即面向服务架构的缩写. SOA是一种范式,目的是增强灵活性.SOA很适宜处理复杂的分布式系统. SOA方法接受异质(不同的平台, ...