当前位置:网站首页>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;
}
}
边栏推荐
- 零知识证明:具有DDH假设的 ZKP
- Chinese enterprise service industry market in 2022
- Redis advantages and data structure related knowledge
- QT - CPP database operation
- C language (high-level) character function and string function + Exercise
- What does real HTAP mean to users and developers?
- MySQL date function
- Swiftui component how to implement textfield of hidden part of phone number mask (tutorial includes source code)
- N32替换STM32,这些细节别忽略!
- 【物理应用】大气吸收损耗附matlab代码
猜你喜欢

Unity 之 切换语言导致报错:System.FormatException:String was not recognized as a valid DateTime.

配置教程:新版本EasyCVR(v2.5.0)组织结构如何级联到上级平台?

现代化个人博客系统 ModStartBlog v5.4.0 登录界面改版,新增联系方式

What does real HTAP mean to users and developers?

Use the self-developed proxy server to solve the cross domain access errors encountered when uploading files by SAP ui5 fileuploader trial version

EasyCVR接入设备后播放视频出现卡顿现象的原因分析及解决
![[R language - basic drawing]](/img/1e/aebf1cbe02c4574671bac6dc2c9171.png)
[R language - basic drawing]

N32 replaces STM32. Don't ignore these details!

我的创作纪念日 -- 2022年7月25日

The switching language of unity causes an error: system FormatException:String was not recognized as a valid DateTime.
随机推荐
C# 之 观察者模式实例 -- 订牛奶
ECS 5 workflow
Haproxy implements proxy configuration
视频融合云服务EasyCVR平台白名单功能如何使用?
N32替换STM32,这些细节别忽略!
2022年暑假ACM热身练习3(详细)
kotlin:Nothing
Interpretation of ue4.25 slate source code
Use the self-developed proxy server to solve the cross domain access errors encountered when uploading files by SAP ui5 fileuploader trial version
kotlin:out in
How long does software testing take?
Kotlin:sealed Class detailed explanation of sealed class
[operation] differences between Oracle, MySQL and sqlserver
Why app uses JSON protocol to interact with server: serialization related knowledge
Open source database innovation in the era of digital economy | the 2022 open atom global open source summit database sub forum was successfully held
面试官:ThreadLocal使用场景有哪些?内存泄露问题如何避免?
What if you don't understand the difference between modularity, componentization and plug-in?
Differences between RDB and AOF for redis persistence
QT - CPP database operation
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