【LeetCode】82. Remove Duplicates from Sorted List II 解题报告(Python&C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/description/
题目描述
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
Example 1:
Input: 1->2->3->3->4->4->5
Output: 1->2->5
Example 2:
Input: 1->1->1->2->3
Output: 2->3
题目大意
在一个有序链表中,如果一个节点的值出现的不止一次,那么把这个节点删除掉。
解题方法
递归
注意审题啊,这个distinct的意思并不是去重,而是删除出现次数不止一次的。
去重的可以看这个题:83. Remove Duplicates from Sorted List
使用递归解法,重点在于找出头结点。如果头结点和第二个节点相等,那么需要一直遍历到第一个和head不相等的节点作为新的头结点,再重复这个过程。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if (!head || !head->next) {
return head;
}
if (head->val != head->next->val) {
head->next = deleteDuplicates(head->next);
} else {
ListNode* move = head->next;
while (move && head->val == move->val) {
move = move->next;
}
return deleteDuplicates(move);
}
return head;
}
};
遍历
说实话,这个非递归的解法写了挺久的。设定了两个指针pre和cur,确保pre节点指向结果链表的尾部,而cur指向当前已经判断多了的链表中重复元素的末尾(若当前不重复,就是该节点),pre->next指向尚未判断的剩余链表的头部。判断是否有重复元素的方法是pre->next和cur是否相等。举例说明:
1. 1(pre,pre->next=2)->2(cur)->3->3->4->4->5
2. 1->2(pre,pre->next=4)->3->3(cur)->4->4->5
3. 1->2(pre,pre->next=5)->3->3->4->4(cur)->5
4. 1->2(pre,pre->next=None)->3->3->4->4->5(cur)
5. 1->2->3->3->4->4->5(pre,pre->next=None)(cur=None)
C++代码如下:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if (!head || !head->next) return head;
ListNode* preHead = new ListNode(0);
preHead->next = head;
ListNode* pre = preHead;
ListNode* cur = head;
while (cur) {
//跳过当前的重复节点,使得cur指向当前重复元素的最后一个位置
while (cur->next && cur->val == cur->next->val) {
cur = cur->next;
}
if (pre->next == cur) {
//pre和cur之间没有重复节点,pre后移
pre = pre->next;
} else {
//pre->next指向cur的下一个位置(相当于跳过了当前的重复元素)
//但是pre不移动,仍然指向之前的链表结尾
pre->next = cur->next;
}
cur = cur->next;
}
return preHead->next;
}
};
字典统计次数
如果忽略有序这个特征,可以统计每个节点出现的次数,判断出现次数是不是1。
第二次遍历的时候,查找下个节点的值出现的次数如果不是1次,那么就删除下个节点。修改这个节点的下个指针指向下下个节点,这是指向该节点位置的指针不要动,因为还要判断新的next值。
python代码如下:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def deleteDuplicates(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
root = ListNode(0)
root.next = head
val_list = []
while head:
val_list.append(head.val)
head = head.next
counter = collections.Counter(val_list)
head = root
while head and head.next:
if counter[head.next.val] != 1:
head.next = head.next.next
else:
head = head.next
return root.next
C++代码如下:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
unordered_map<int, int> m;
ListNode dummy(0);
ListNode* dummy_move = &dummy;
ListNode* move = head;
while (move) {
m[move->val]++;
move = move->next;
}
move = head;
while (move) {
if (m[move->val] == 1) {
dummy_move->next = move;
dummy_move = dummy_move->next;
}
move = move->next;
}
dummy_move->next = nullptr;
return dummy.next;
}
};
参考资料:https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/discuss/28335/My-accepted-Java-code
日期
2018 年 6 月 23 日 ———— 美好的周末要从刷题开始
【LeetCode】82. Remove Duplicates from Sorted List II 解题报告(Python&C++)的更多相关文章
- [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: Remove Duplicates from Sorted List II 解题报告
Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...
- [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 Array II
Problem: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? F ...
- 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】80. Remove Duplicates from Sorted Array II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【原创】leetCodeOj ---Remove Duplicates from Sorted List II 解题报告
明日深圳行,心情紧张,写博文压压惊 囧 ------------------------------------- 原题地址: https://oj.leetcode.com/problems/rem ...
- 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题要求 ...
随机推荐
- ubuntu终端ls颜色配置
buntu中没有LS_COLORS,/etc/目录中也没有DIR_COLORS,所以这里使用dircolor命令加以解决 1. 利用dircolors命令,查看我们的系统当前的文件名称显示颜色的值,然 ...
- 判断是否有重复,判断字符串是否有重复汉字【c#】
string corn = "公司"; int n = 0; if (tbCorporateName.Text.IndexOf(corn) > -1) { string co ...
- A Child's History of England.52
'Arthur,' said the King, with his wicked eyes more on the stone floor than on his nephew, 'will you ...
- mybatis-plus条件构造用is开头的Boolean类型字段时遇到的问题
is打头的Boolean字段导致的代码生成器与lambda构造器的冲突 https://gitee.com/baomidou/mybatis-plus/issues/I171DD?_from=gite ...
- ACE_Message_Block实现浅析
ACE_Message_Block实现浅析1. 概述ACE_Message_Block是ACE中很重要的一个类,和ACE框架中的重要模式的实现 如ACE_Reactor, ACE_Proactor, ...
- java通过反射获取Java对象属性值
说明: 作为反射工具类,通过对象和属性的名字获取对象属性的值,如果在当前对象属性没有找到,依次向上收集所有父类的属 性,直到找到属性值,没有找到返回null: 代码: 1.classUtil pack ...
- SpringBoot中使用JUnit4(入门篇)
添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>sp ...
- 【C/C++】拔河比赛/分组/招商银行
题目:小Z组织训练营同学进行一次拔河比赛,要从n(2≤n≤60,000)个同学中选出两组同学参加(两组人数可能不同).对每组同学而言,如果人数超过1人,那么要求该组内的任意两个同学的体重之差的绝对值不 ...
- HDC2021技术分论坛:进程崩溃/应用卡死,故障频频怎么办?
作者:jiwenqiang,DFX技术专家 提到开发一个产品,我们通常首先想到的是要实现什么样的功能,但是除了功能之外,非功能属性也会很大程度上影响一个产品的体验效果,比如不定时出现的应用卡死.崩溃 ...
- Oracle命名规则
1.长度不能超过三十个字符 2. 不要使用Oracle关键字 比如:id name table 3. 不能使用数字开头 包含:数字 字母 下划线 美元符号 4. 建议用 英文单词,不要去用中 ...