当前位置:网站首页>[C language brush questions] explanation of linked list application
[C language brush questions] explanation of linked list application
2022-07-28 20:50:00 【Choice~】
Catalog
NC25 Delete duplicate elements in the ordered linked list -I
Method 1 : Traverse delete ( Recommended )
Recommend an artifact to everyone Cattle from The following questions and methods are available to Niuke , And enterprise interview high-frequency questions

NC25 Delete duplicate elements in the ordered linked list -I
describe
Delete the duplicate elements in the given linked list ( The elements in the linked list are ordered from small to large ), Make all elements in the linked list appear only once
for example :
The list given is 1→1→2, return 1→2.
The list given is 1→1→2→3→3, return 1→2→3.
Data range : The length of the linked list meets 0≤n≤100, The value of any node in the linked list satisfies val∣≤100
Advanced : Spatial complexity O(1)O(1), Time complexity O(n)O(n)

We can get such information in the title :
- Given a small to large ordered list
- Delete the repeated elements in the linked list , Each value leaves only one node
Method 1 : Traverse delete ( Recommended )
Ideas :
Since only one element is left in succession , Which one is the best for us to stay ? Of course, it is the first element encountered !
if(cur->val==cut->next->val)
cur->var=cur->next->next;Because the first element is directly connected with the previous linked list node , Don't worry about the front , It only needs Skip repeat after The elements of , Just connect the first non repeating element , It is always more convenient to connect the following elements in the linked list than the previous elements , Because you cannot access in reverse order .
specific working means :
- step 1: Judge whether the linked list is empty , Empty linked list is returned directly without processing .
- step 2: Use a pointer to traverse the linked list , If the current node of the pointer has the same value as the next node , Let's skip the next node , The current node is directly connected to the next node .
- step 3: If the value of the current node is different from that of the next node , Continue to traverse back .
- step 4: Two node values are used each time during the cycle , To check whether two consecutive nodes are empty .

Moving pictures show :
Core code
struct ListNode* deleteDuplicates(struct ListNode* head ) {
if(head == NULL || head->next == NULL) return head;
struct ListNode *p=head,*pn = head->next;
while(p){
if(p->val == pn->val){
while(pn&&pn->val == p->val) // Judge
pn = pn->next;
p->next = pn;// Delete
}
p = p->next;
}
return head;
// write code here
}
Complexity analysis :
- Time complexity :O(n)O(n)O(n), among nnn Is the length of the list , Traverse the list once
- Spatial complexity :O(1)O(1)O(1), Constant level pointer variables use , No additional auxiliary space is used
Method 2 : Recursive solution
The core idea :
Use the idea of recursion to solve . Each recursion requires comparing and deleting duplicate elements . The idea of this time is very similar to recursive inverse linked list , The only difference is the processing logic . The processing logic of deleting duplicate linked lists is to delete the current node every time ( If the current node is the same as the next node )
Core code :
ListNode* deleteDuplicates(ListNode* head) {
if (head == nullptr || head->next == nullptr) // When a node does not exist or there is only one node , Recursion ends
return head;
head->next = deleteDuplicates(head->next); // Recursive body , Each recursion starts at the next node
if (head->val == head->next->val) // Compare whether the values of the current element and the next element are equal , If equal, delete the current node directly
head = head->next; //head The pointer points to the next node , Indicates that the current node is missing , It no longer belongs to this linked list
return head; // Return the result of this recursion
} Complexity analysis :
recursive n Time , So the time complexity is zero O(N). Each recursion requires an additional variable to be saved , So the space complexity is zero O(N), Time complexity O(n)
Reverse a linked list
describe
Given the head node of a single linked list pHead( The header node has a value , For example, in the figure below , its val yes 1), The length is n, After reversing the linked list , Return the header of the new linked list .
Data range :0≤n≤1000
requirement : Spatial complexity O(1)O(1) , Time complexity O(n)O(n) .
For example, when entering a linked list {1,2,3} when ,
After reversal , The original linked list becomes {3,2,1}, So the corresponding output is {3,2,1}.
The above conversion process is shown in the figure below :


solution : iteration
- When traversing a linked list , Set the... Of the current node next The pointer changes to point to the previous node . Because the node does not refer to its previous node , So you have to store its previous node in advance . Before changing the reference , You also need to store the latter node . Finally, the new header reference is returned .
- The illustration :

Complexity analysis :
Time complexity :O(N), among N It's the length of the list . You need to traverse the linked list once .
Spatial complexity :O(1), Constant space complexity
Code display :
```/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*
* C Language declaration defines global variables. Please add static, Prevent duplicate definitions
*/
struct ListNode* ReverseList(struct ListNode* pHead ) {
if(!pHead)return NULL;
if(pHead->next==NULL)return pHead;
struct ListNode *p,*t;
t=pHead->next;
p=pHead;p->next=NULL;
while(t){
p=t;
t=t->next;
p->next=pHead;
pHead=p;
}
return p;
}
Click the link on the right Cattle from - Looking for work artifact | Written examination question bank | The interview experience | Internship recruitment push , One stop job search _ Cattle from

边栏推荐
- PL515 SOT23-5 单/双口 USB 充电协议端口控制器 百盛电子代理商
- MySQL batch update data
- Thinking and summary of R & D Efficiency
- JS page black and white background switch JS special effect
- Three deletion strategies and eviction algorithm of redis
- Raspberry pie 4B uses MNN to deploy yolov5 Lite
- 阿里云 MSE 支持 Go 语言流量防护
- [pytorch] LSTM neural network
- Unity gadget displays the file size of the resource directory
- How to use redis to realize things and locks?
猜你喜欢

How to balance security and performance in SQL?

卡通js射击小游戏源码

Raspberry pie 4B deploy yolov5 Lite using ncnn

PL515 SOT23-5 单/双口 USB 充电协议端口控制器 百盛电子代理商

想画一张版权属于你的图吗?AI作画,你也可以

JS picture hanging style photo wall JS special effect

太空射击第11课: Sound and Music

LVM logical volume

Redis入门二:redhat 6.5安装使用

Unity typewriter teaches you three ways
随机推荐
【ADB常用命令及其用法大全(来自[醒不了的星期八]的全面总结)】
Subcontracting loading of wechat applet
“当你不再是程序员,很多事会脱离掌控”—— 对话全球最大独立开源公司SUSE CTO...
研发效能的思考总结
太空射击第09课:精灵动画
Redis 3.0 source code analysis - data structure and object SDS list Dict
FPGA programming experience
Linxu [basic instructions]
Use of DDR3 (axi4) in Xilinx vivado (2) read write design
Use of DDR3 (axi4) in Xilinx vivado (3) module packaging
【服务器数据恢复】HP StorageWorks系列存储RAID5两块盘故障离线的数据恢复案例
Integrating database Ecology: using eventbridge to build CDC applications
How do we do full link grayscale on the database?
LeetCode_ Bit operation_ Medium_ 260. Number III that appears only once
Leetcode:2141. The longest time to run n computers at the same time [the maximum value is two points]
7/27 训练日志(位运算+后缀数组)
[1331. Array serial number conversion]
超大模型工程化实践打磨,百度智能云发布云原生AI 2.0方案
[complete collection of common ADB commands and their usage (from a comprehensive summary of [wake up on Sunday)]
MySQL error: specified key was too long; max key length is 767 bytes