当前位置:网站首页>[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

边栏推荐
- Cartoon JS shooting game source code
- 太空射击第14课: 玩家生命
- Use order by to sort
- Use of DDR3 (axi4) in Xilinx vivado (4) incentive design
- [pytorch] LSTM neural network
- 一文了解 Rainbond 云原生应用管理平台
- GIS数据漫谈(六)— 投影坐标系统
- TCP.IP
- js图片悬挂样式照片墙js特效
- Prize essay solicitation | 2022 cloud native programming challenge draft activity opens
猜你喜欢

【服务器数据恢复】HP StorageWorks系列存储RAID5两块盘故障离线的数据恢复案例

JS fly into JS special effect pop-up login box

js图表散点图例子

Nat experiment demonstration (Huawei switch equipment configuration)

Pl515 SOT23-5 single / Dual Port USB charging protocol port controller Parkson electronic agent

Ask if you don't understand, and quickly become an advanced player of container service!

js图片悬挂样式照片墙js特效

js win7透明桌面切换背景开始菜单js特效

Classes and objects (medium)
![Leetcode:2141. The longest time to run n computers at the same time [the maximum value is two points]](/img/33/05620dfff2f6ac67691d20c5256c5b.png)
Leetcode:2141. The longest time to run n computers at the same time [the maximum value is two points]
随机推荐
Integrating database Ecology: using eventbridge to build CDC applications
Use order by to sort
TCP.IP
EasyNLP中文文图生成模型带你秒变艺术家
Unity package project to vs deploy hololens process summary
类与对象(中)
阿里云 MSE 支持 Go 语言流量防护
Random talk on GIS data (VI) - projection coordinate system
JS chart scatter example
Seventeen year operation and maintenance veterans, ten thousand words long, speak through the code of excellent maintenance and low cost~
【ADB常用命令及其用法大全(来自[醒不了的星期八]的全面总结)】
NAT实验演示(Huawei交换机设备配置)
Speech controlled robot based on ROS (I): realization of basic functions
Easynlp Chinese text and image generation model takes you to become an artist in seconds
[pytorch] LSTM neural network
Unity performance optimization scheme arrangement
Linxu [permission, sticky bit]
Introduction to redis II: RedHat 6.5 installation and use
Shanghai Jiaotong University joined hands with Taobao to set up a media computing laboratory: promoting the development of key technologies such as video super score
How bad can a programmer be? Nima, they are all talents