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

边栏推荐
- 【1331. 数组序号转换】
- 全链路灰度在数据库上我们是怎么做的?
- Redis入门二:redhat 6.5安装使用
- Mongoose condition queries part of the data of the specified attribute
- DHCP.DNS.NFS
- Clock distribution of jesd204 IP core (ultrascale Series)
- Record an error in runtime. Getruntime().Exec ("command")
- 瀚高数据库最佳实践配置工具HG_BP日志采集内容
- Hangao database best practice configuration tool Hg_ BP log collection content
- Network layer performance test
猜你喜欢

Unity object path query tool

有奖征文 | 2022 云原生编程挑战赛征稿活动开启

远光软件获得阿里云产品生态集成认证,携手阿里云共建新合作

Three deletion strategies and eviction algorithm of redis

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

JS picture hanging style photo wall JS special effect

Explain the life cycle function in unity in detail

融合数据库生态:利用 EventBridge 构建 CDC 应用

Unity typewriter teaches you three ways

Thinking and summary of R & D Efficiency
随机推荐
[complete collection of common ADB commands and their usage (from a comprehensive summary of [wake up on Sunday)]
Unity performance optimization
Redis的三种删除策略以及逐出算法
Three steps to teach you unity serial communication
What is data center? What value does the data center bring_ Light spot technology
微信小程序的分包加载
全链路灰度在数据库上我们是怎么做的?
Network shell
Three deletion strategies and eviction algorithm of redis
Unity performance optimization scheme arrangement
LVS deployment Dr cluster
flask 静态文件服务搭建
太空射击第09课:精灵动画
FPGA programming experience
[1331. Array serial number conversion]
"When you are no longer a programmer, many things will get out of control" -- talk to SUSE CTO, the world's largest independent open source company
使用ORDER BY 排序
DHCP.DNS.NFS
一文读懂Okaleido Tiger近期动态,挖掘背后价值与潜力
JS drag and drop alert pop-up plug-in