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 ...
随机推荐
- 6-9 😢 5小时的debug: 从rails 命令运行超慢开始->删除rails->删除ruby->删除rvm->安装上rvm->安装上ruby
上午,莫名其妙的rails app不能用了,rails -v一查发现不存在.ruby -v发现是2.0的版本.很着急上火,因为很少使用过rvm这个ruby版本控制器.所以照官网文档.从新安装ruby, ...
- a标记无效问题
当在<a href=''></a>这个标记中嵌入<td></td> 就会导致部分浏览器无法单击,所以在开发HTML页面的时候,一定不要在 a标记中嵌入 ...
- Invalid bound statement (not found)错误的可能原因
其他原因导致此问题解决参考: 1.检查xml文件所在package名称是否和Mapper interface所在的包名 <mapper namespace="me.tspace.pm. ...
- HTTP相关知识点
一.工作原理 HTTP协议工作于客户端-服务器架构上.浏览器作为HTTP客户端通过URL向HTTP服务端(即web服务端)发送所有请求. Web服务器接收到请求后,向客户端发送相应的响应信 ...
- Chrome插件(Extensions)开发实践
内容摘自:http://www.cnblogs.com/mfryf/p/3701801.html
- bzoj1074
题意: 给你n次折叠 m个询问 每次询问折叠后,xi,yi有几层 题解: 计算几何 模拟 #include<cstdio> #include<cstdlib> #include ...
- java中break,continue,标签实现goto效果(编程思想)
goto 编程语言中一开始就有goto关键词了.事实上,goto起源于汇编语言的程序控制:“若条件A成立,则调到这里:否则跳到那里”. goto语句时在源码级别上的跳转,这导致了其不好的名誉.于是go ...
- 『转』G Data InternetSecurity 2014 – 免费3个月
G Data来自德国的顶级杀毒软件,采用BitDefender+CloseGap双引擎,屡获AV-TEST防護率100%.不多介绍,目前2014中文版没有上市.活动地址:点此进入官方网站:点此进入申请 ...
- 201621123010《Java程序设计》第3周学习总结
1.本周学习总结 初学面向对象,会学习到很多碎片化的概念与知识.尝试学会使用思维导图将这些碎片化的概念.知识点组织起来.请使用工具画出本周学习到的知识点及知识点之间的联系.步骤如下: 1.1 写出你认 ...
- 探究js正则匹配方法:match和exec
match是字符串方法,写法为:str.match(reg) exec是正则表达式方法,写法为:reg.exec(str) match和exec在匹配成功时返回的都是数组,在没有匹配上时返回的都是nu ...