当前位置:网站首页>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;
}
}
边栏推荐
- Qt: 一个SIGNAL绑定多个SLOT
- 2022 Hangdian multi school field 2 1011 DOS card (line segment tree)
- BM14 链表的奇偶重排
- Is zero basic software testing training reliable?
- QT - CPP database operation
- A priori, a posteriori, likelihood
- Mongodb initialization
- Is two months of software testing training reliable?
- Decimal to binary advanced version (can convert negative numbers and boundary values)
- Is the prospect of software testing dead? Has it entered the cold winter?
猜你喜欢

4 年后,Debian 终夺回“debian.community”域名!

kotlin:Nothing
![[R language - basic drawing]](/img/1e/aebf1cbe02c4574671bac6dc2c9171.png)
[R language - basic drawing]

112. Use the self-developed proxy server to solve the cross domain access error encountered when uploading files by SAP ui5 fileuploader

【图像分割】基于方向谷形检测实现静脉纹路分割附MATLAB代码

11 年膨胀 575 倍,微信为何从“小而美”变成了“大而肥”?

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

EasyCVR接入设备后播放视频出现卡顿现象的原因分析及解决

【物理应用】水下浮动风力涡轮机的尾流诱导动态模拟风场附matlab代码

My creation anniversary -- July 25th, 2022
随机推荐
[GXYCTF2019]StrongestMind
Pytorch GPU yolov5 reports an error
New upgrade! The 2022 white paper on cloud native architecture was released
湖上建仓全解析:如何打造湖仓一体数据平台 | DEEPNOVA技术荟系列公开课第四期
JVM four reference types
Qt: 一个SIGNAL绑定多个SLOT
1、 My first wechat applet
If you want to change to it, does it really matter if you don't have a major?
SwiftUI Swift 之正向地理编码与反向地理编码(教程含源码)
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
How to write a JMeter script common to the test team
112. Use the self-developed proxy server to solve the cross domain access error encountered when uploading files by SAP ui5 fileuploader
Introduction and advanced MySQL (7)
Xiaobai must see the development route of software testing
Overview and working principle of single chip microcomputer crystal oscillator
Live broadcast platform software development, JS implementation by alphabetical order
Win11系统svchost.exe一直在下载怎么办?
2022 Hangdian multi school field 2 1011 DOS card (line segment tree)
UE4.25 Slate源码解读
OAI L3 and L2 interface analysis