当前位置:网站首页>BM16 delete duplicate elements in the ordered linked list -ii
BM16 delete duplicate elements in the ordered linked list -ii
2022-07-28 19:10:00 【Love life and programming a】
1 Topic introduction


Solution 1 :
Establish the sentinel node and then traverse the linked list to connect the unique node to the sentinel node
Time complexity :O(n), among n Is the number of nodes in the linked list , Only one traversal
Spatial complexity :O(1), Only temporary pointers are opened up , Constant order space
public class Solution {
/** * * @param head ListNode class * @return ListNode class */
public ListNode deleteDuplicates (ListNode head) {
// write code here
if(head == null){
return null;
}
// 1: Define two pointers , An element that appears only once , One is used to connect the element
ListNode ret = new ListNode(1);
ListNode tep = ret;
ListNode cur = ret;
cur.next = head;
cur = cur.next;
while(cur != null && cur.next != null){
// If not, it means cur It points to the only element
if(cur.val != cur.next.val){
tep.next = cur;
tep = tep.next;
cur = cur.next;
}else{
// If they are equal, go on until you find the unequal element positions
while(cur != null && cur.next != null && cur.val == cur.next.val){
cur = cur.next;
}
// take cur Pointer to the next element , if cur If it is empty, the following elements are all found , At this time, you need to manually connect the null pointer , Otherwise, it will jump out of the loop
cur = cur.next;
if(cur == null){
tep.next = null;
}
}
}
// If traversal is completed cur In the last element , Then you need to manually connect the last element , Because it's in ascending order , It means that the last element must be unique
if(cur != null && cur.next == null){
tep.next = cur;
}
return ret.next;
}
}
Solution 2 :
Use the idea of recursion to solve
Time complexity :O(n)
Spatial complexity :O(1)
public class Solution {
/** * * @param head ListNode class * @return ListNode class */
public ListNode deleteDuplicates(ListNode head) {
if(head == null){
return null;
}
if(head.next != null && head.val == head.next.val){
// Duplicate values found
while(head.next != null && head.val == head.next.val){
head = head.next;// Delete
}
return deleteDuplicates(head.next);// Delete the same node as the deleted node
}
head.next = deleteDuplicates(head.next);// When no duplicate values are found , Just keep doing recursive operations
return head;
}
}
Solution 3 :
utilize hashmap Do two iterations , Record the number of occurrences of each number for the first time , The second time, create a new sentinel node to connect the unique node .
Time complexity :O(n)
Spatial complexity :O(n)This method is applicable to the case that the order of the linked list is random
public class Solution {
public ListNode deleteDuplicates (ListNode head) {
// Empty list
if(head == null)
return null;
HashMap<Integer,Integer> mp = new HashMap<>();
ListNode cur = head;
// Traverse the linked list and count the number of occurrences of each node value
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);
// Add a header before the linked list
res.next = head;
cur = res;
// Traverse the list again
while(cur.next != null){
// If the node value count is not 1
if(mp.get(cur.next.val) != 1)
// Delete this node
cur.next = cur.next.next;
else
cur = cur.next;
}
// Remove the meter head
return res.next;
}
}
边栏推荐
- The difference between --save Dev and --save in NPM
- kotlin:Nothing
- EasyCVR新版本级联时,下级平台向上传递层级目录显示不全的原因分析
- QT user defined control user guide (flying Qingyun)
- 历史上的今天:微软收购 QDOS;模型检测先驱出生;第一张激光照排的中文报纸...
- 【图像分割】基于方向谷形检测实现静脉纹路分割附MATLAB代码
- 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
- 2022年牛客多校第2场 J . Link with Arithmetic Progression (三分+枚举)
- 服务器正文21:不同编译器对预编译的处理(简单介绍msvc和gcc)
猜你喜欢

QT user defined control user guide (flying Qingyun)

真正的 HTAP 对用户和开发者意味着什么?

Easynlp Chinese text and image generation model takes you to become an artist in seconds

行业落地呈现新进展 | 2022开放原子全球开源峰会OpenAtom OpenHarmony分论坛圆满召开

C language (high-level) character function and string function + Exercise

Why did wechat change from "small and beautiful" to "big and fat" when it expanded 575 times in 11 years?

CTR click through rate prediction practice project of advertising recommendation!

AI 改变千行万业,开发者如何投身 AI 语音新“声”态

1、 My first wechat applet

New progress in the implementation of the industry | the openatom openharmony sub forum of the 2022 open atom global open source summit was successfully held
随机推荐
OAI L3 and L2 interface analysis
Win11怎么调亮度?Win11调屏幕亮度的四种方法
When the new version of easycvr is linked at the same level, the subordinate platform passes up the cause analysis of the incomplete display of the hierarchical directory
Special Lecture 6 tree DP learning experience (long-term update)
服务器正文21:不同编译器对预编译的处理(简单介绍msvc和gcc)
Open source database innovation in the era of digital economy | the 2022 open atom global open source summit database sub forum was successfully held
What kind of knowledge payment system functions are more conducive to the development of the platform and lecturers?
湖上建仓全解析:如何打造湖仓一体数据平台 | DEEPNOVA技术荟系列公开课第四期
QT function optimization: QT 3D gallery
112. Use the self-developed proxy server to solve the cross domain access error encountered when uploading files by SAP ui5 fileuploader
【数据分析】基于MATLAB实现SVDD决策边界可视化
Can I get employed after two months of software testing training?
1、 My first wechat applet
配置教程:新版本EasyCVR(v2.5.0)组织结构如何级联到上级平台?
关于ASM冗余问题
EasyCVR设备离线后无法再次上线该如何解决?
现代化个人博客系统 ModStartBlog v5.4.0 登录界面改版,新增联系方式
Minio distributed file system learning notes
What is one hot code? Why use it and when?
Swiftui swift forward geocoding and reverse geocoding (tutorial with source code)