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 ...
随机推荐
- FastJson/spring boot: json输出方法二
1.引入FastJson依赖包 <!-- FastJson --> <dependency> <groupId>com.alibaba</groupId> ...
- 雷林鹏分享:Ruby CGI Cookies
Ruby CGI Cookies HTTP协议是无状态协议.但对于一个商业网站,它需要保持不同的页面间的会话信息. 如用户在网站注册过程中需要跳转页面,但又要保证之前填写的信息部丢失. 这种情况下 C ...
- 安装pip最简单的方法
http://blog.csdn.net/lyj_viviani/article/details/70568434
- 6-9 😢 5小时的debug: 从rails 命令运行超慢开始->删除rails->删除ruby->删除rvm->安装上rvm->安装上ruby
上午,莫名其妙的rails app不能用了,rails -v一查发现不存在.ruby -v发现是2.0的版本.很着急上火,因为很少使用过rvm这个ruby版本控制器.所以照官网文档.从新安装ruby, ...
- hdu4292网络流dinic
因为数组开小了,导致tle了一整天:( tle的几点原因:http://blog.csdn.net/ameir_yang/article/details/53698478 思路都是对的,把每个人进行拆 ...
- uva-1636-概率
https://vjudge.net/problem/UVA-1636 给出一个左轮手枪的弹夹串,第一枪是空的,问是继续打还是转一转再打下一枪还为空的概率大.继续打为空的概率就是 '00'的个数比上' ...
- js控制easyui datagrid列的显示和隐藏
easyui datagrid 列隐藏和显示 $('#grid').datagrid('hideColumn','列field'); //把hideColumn换成showColumn 即为显示列
- 201621123005《Java程序设计》实验总结
201621123005<Java程序设计>第七次实验总结 1. 本周学习总结 1.1 思维导图:Java图形界面总结 2.书面作业 1. GUI中的事件处理 1.1 写出事件处理模型中最 ...
- openGL之着色器程序的使用
#define GLEW_STATIC #include <GL\glew.h> #include <GLFW\glfw3.h> #include<iostream> ...
- 仿智能社官网:原生JS实现简单又酷炫的3D立方体时钟
先放一下我做的效果:https://linrunzheng.github.io/3Dclock/3Dclock/new.html 至于3D立方体怎么做这里就不在阐述了,可以看一下我之前的博客. 这里默 ...