当前位置:网站首页>BM16 删除有序链表中重复的元素-II
BM16 删除有序链表中重复的元素-II
2022-07-28 17:13:00 【爱生活爱编程a】
1 题目介绍


解法一:
建立哨兵节点然后遍历链表将唯一节点连接到哨兵节点上
时间复杂度:O(n),其中n为链表节点数,只有一次遍历
空间复杂度:O(1),只开辟了临时指针,常数级空间
public class Solution {
/** * * @param head ListNode类 * @return ListNode类 */
public ListNode deleteDuplicates (ListNode head) {
// write code here
if(head == null){
return null;
}
// 1: 定义两个指针,一个用来找只出现一次的元素,一个用来连接该元素
ListNode ret = new ListNode(1);
ListNode tep = ret;
ListNode cur = ret;
cur.next = head;
cur = cur.next;
while(cur != null && cur.next != null){
// 若不相等说明cur指向的就是唯一元素
if(cur.val != cur.next.val){
tep.next = cur;
tep = tep.next;
cur = cur.next;
}else{
// 若相等则继续往前走直达找到不相等的元素位置
while(cur != null && cur.next != null && cur.val == cur.next.val){
cur = cur.next;
}
// 将cur指针指向下一个元素,若cur为空了说明后面的元素都找完了,此时需手动将空指针连接上去,否则就会跳出循环
cur = cur.next;
if(cur == null){
tep.next = null;
}
}
}
// 若遍历完cur位于最后一个元素,那么就需要手动将最后一个元素也连接上,因为是升序排列,能走到这里说明最后一个元素一定是唯一的
if(cur != null && cur.next == null){
tep.next = cur;
}
return ret.next;
}
}
解法二:
利用递归的思想解决
时间复杂度:O(n)
空间复杂度:O(1)
public class Solution {
/** * * @param head ListNode类 * @return ListNode类 */
public ListNode deleteDuplicates(ListNode head) {
if(head == null){
return null;
}
if(head.next != null && head.val == head.next.val){
//发现有重复值
while(head.next != null && head.val == head.next.val){
head = head.next;//删除
}
return deleteDuplicates(head.next);//把与删除的那个结点相同的结点也进行删除
}
head.next = deleteDuplicates(head.next);//当没有发现重复值的情况,就一直进行递归操作
return head;
}
}
解法三:
利用hashmap进行两次遍历,第一遍记录每个数出现的次数,第二遍新建哨兵节点连接唯一节点.
时间复杂度:O(n)
空间复杂度:O(n)此方法适用于链表顺序是随机的情况下
public class Solution {
public ListNode deleteDuplicates (ListNode head) {
//空链表
if(head == null)
return null;
HashMap<Integer,Integer> mp = new HashMap<>();
ListNode cur = head;
//遍历链表统计每个节点值出现的次数
while(cur != null){
if(mp.containsKey(cur.val))
mp.put(cur.val, (int)mp.get(cur.val) + 1);
else
mp.put(cur.val,1);
cur = cur.next;
}
ListNode res = new ListNode(0);
//在链表前加一个表头
res.next = head;
cur = res;
//再次遍历链表
while(cur.next != null){
//如果节点值计数不为1
if(mp.get(cur.next.val) != 1)
//删去该节点
cur.next = cur.next.next;
else
cur = cur.next;
}
//去掉表头
return res.next;
}
}
边栏推荐
- QT with line encoding output cout
- 【数据分析】基于MATLAB实现SVDD决策边界可视化
- How new people get started learning software testing
- Why did wechat change from "small and beautiful" to "big and fat" when it expanded 575 times in 11 years?
- Introduction and advanced level of MySQL (II)
- 2022年牛客多校第2场 J . Link with Arithmetic Progression (三分+枚举)
- LeetCode_ 63_ Different paths II
- How to break through the bottleneck of professional development for software testing engineers
- Is there any prospect and way out for software testing?
- The wechat installation package has expanded 575 times in 11 years, and the up owner: "98% of the documents are garbage"; Apple App store was exposed to a large number of pornographic apps; Four techn
猜你喜欢

Xiaobai must see the development route of software testing

Full analysis of warehouse building on the lake: how to build a lake warehouse integrated data platform | deepnova technology collection series open class phase IV

EasyCVR新版本级联时,下级平台向上传递层级目录显示不全的原因分析

Win11电脑摄像头打开看不见,显示黑屏如何解决?

Introduction and advanced level of MySQL (I)

What kind of knowledge payment system functions are more conducive to the development of the platform and lecturers?

3、 Uni app fixed or direct to a certain page

Configuration tutorial: how does the organizational structure of the new version of easycvr (v2.5.0) cascade to the superior platform?

Redis cache avalanche, penetration, breakdown, bloom filter, detailed explanation of distributed lock

4 年后,Debian 终夺回“debian.community”域名!
随机推荐
Kotlin:Sealed class密封类详解
4 年后,Debian 终夺回“debian.community”域名!
Why app uses JSON protocol to interact with server: serialization related knowledge
How much is software testing training generally?
Four years later, Debian finally recaptured the "debian.community" domain name!
LeetCode_ 96_ Different binary search trees
AI has changed thousands of industries. How can developers devote themselves to the new "sound" state of AI voice
Kali doesn't have an eth0 network card? What if you don't connect to the Internet
什么样的知识付费系统功能,更有利于平台与讲师发展?
Why did wechat change from "small and beautiful" to "big and fat" when it expanded 575 times in 11 years?
My creation anniversary -- July 25th, 2022
Is the software testing industry really saturated?
What if you don't understand the difference between modularity, componentization and plug-in?
How to solve the problem that easycvr device cannot be online again after offline?
视频融合云服务EasyCVR平台白名单功能如何使用?
EasyCVR设备离线后无法再次上线该如何解决?
Leetcode binary tree class
N32替换STM32,这些细节别忽略!
OAI L3 and L2 interface analysis
MySQL date function