当前位置:网站首页>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;
}
}
边栏推荐
- 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
- Can zero basis software testing work?
- How to solve the problem that easycvr device cannot be online again after offline?
- Is the software testing training institution reliable?
- What kind of knowledge payment system functions are more conducive to the development of the platform and lecturers?
- 2022 Hangdian multi school field 2 1011 DOS card (line segment tree)
- CTR click through rate prediction practice project of advertising recommendation!
- OAI L3 and L2 interface analysis
- Interviewer: what are the usage scenarios of ThreadLocal? How to avoid memory leakage?
- QT widget promoted to QWidget
猜你喜欢

What is the future of software testing? How to learn?

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

历史上的今天:微软收购 QDOS;模型检测先驱出生;第一张激光照排的中文报纸...

cv5200无线WiFi通信模块,视频图像传输无线化,实时无线通信技术

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

How to write a JMeter script common to the test team

How to obtain data on mobile phones and web pages after the SCM data is uploaded to Alibaba cloud Internet of things platform?

When unity customizes the editor, let the subclass inherit the inspector display effect of the parent class

DevCon.exe 导出output至指定文件

Decimal to binary advanced version (can convert negative numbers and boundary values)
随机推荐
How to obtain data on mobile phones and web pages after the SCM data is uploaded to Alibaba cloud Internet of things platform?
Use the self-developed proxy server to solve the cross domain access errors encountered when uploading files by SAP ui5 fileuploader trial version
BM16 删除有序链表中重复的元素-II
How to solve the problem that the win11 computer camera cannot be seen when it is turned on and the display screen is black?
Swiftui component how to implement textfield of hidden part of phone number mask (tutorial includes source code)
My creation anniversary -- July 25th, 2022
How big is it suitable for learning software testing?
Is it easy to learn the zero foundation of software testing?
【雷达】基于核聚类实现雷达信号在线分选附matlab代码
服务器正文21:不同编译器对预编译的处理(简单介绍msvc和gcc)
Overview and working principle of single chip microcomputer crystal oscillator
Can the training software test be employed
QT & OpenGL lighting
QT function optimization: QT 3D gallery
视频融合云服务EasyCVR平台白名单功能如何使用?
Attention mechanism and code implementation
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
BM14 链表的奇偶重排
Structure and working principle of thyristor
[actual combat] realize page distortion correction with OpenCV