【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题要求 ...
随机推荐
- LetNet、Alex、VggNet分析及其pytorch实现
简单分析一下主流的几种神经网络 LeNet LetNet作为卷积神经网络中的HelloWorld,它的结构及其的简单,1998年由LeCun提出 基本过程: 可以看到LeNet-5跟现有的conv-& ...
- 分布式事务(3)---强一致性分布式事务Atomikos实战
分布式事务(1)-理论基础 分布式事务(2)---强一致性分布式事务解决方案 分布式事务(4)---最终一致性方案之TCC 前面介绍强一致性分布式解决方案,这里用Atomikos框架写一个实战的dem ...
- 学习java的第二十八天
一.今日收获 1.java完全学习手册第三章算法的3.2排序,比较了跟c语言排序上的不同 2.观看哔哩哔哩上的教学视频 二.今日问题 1.快速排序法的运行调试多次 2.哔哩哔哩教学视频的一些术语不太理 ...
- 【leetcode】170. Two Sum III - Data structure design 两数之和之三 - 数据结构设计
Design and implement a TwoSum class. It should support the following operations: add and find. add ...
- 2021广东工业大学新生赛决赛 L-歪脖子树下的灯
题目:L-歪脖子树下的灯_2021年广东工业大学第11届腾讯杯新生程序设计竞赛(同步赛) (nowcoder.com) 比赛的时候没往dp这方面想(因为之前初赛和月赛数学题太多了啊),因此只往组合数学 ...
- Linux定时任务crontable简介
Linux下定时执行任务的方法:Linux之crond 服务介绍:https://www.cnblogs.com/liang-io/p/9596294.html http://www.mamicode ...
- Spring整合Ibatis之SqlMapClientDaoSupport
前言 HibernateDaoSupport SqlMapClientDaoSupport . 其实就作用而言两者是一样的,都是为提供DAO支持,为访问数据库提供支持. 只不过HibernateD ...
- Dubbo多协议支持
除了Dubbo服务暴露协议Dubbo协议外,Dubbo框架还支持另外8种服务暴露协议:RMI协议.Hessian协议.HTTP协议.WebService协议.Thrift协议.Memcached协议. ...
- 【编程思想】【设计模式】【创建模式creational 】工厂模式factory_method
Python版 https://github.com/faif/python-patterns/blob/master/creational/factory_method.py #!/usr/bin/ ...
- Oracle 表结构管理
表其实是数据的'容器'.oracle有几种类型的表: 普通表(ordinary table)又叫堆组织表. 聚簇表(clustered table) 分区表(partition table) 外部表( ...