当前位置:网站首页>[leetcode19] delete the penultimate node in the linked list
[leetcode19] delete the penultimate node in the linked list
2022-07-06 12:24:00 【Vigorous waist Nuo dance】
Personally to
Front and rear double pointers
subject
I'll give you a list , Delete the last of the linked list n Nodes , And return the head node of the list .
Example 1:
Input :head = [1,2,3,4,5], n = 2
Output :[1,2,3,5]
Example 2:
Input :head = [1], n = 1
Output :[]
Example 3:
Input :head = [1,2], n = 1
Output :[1]
Tips :
The number of nodes in the list is sz
1 <= sz <= 30
0 <= Node.val <= 100
1 <= n <= sz
Advanced : Can you try a scan implementation ?
source : Power button (LeetCode)
link :https://leetcode.cn/problems/remove-nth-node-from-end-of-list
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
class Solution {
public:
/* The method is to define two pointers . The phase difference interval is constant n. And advance simultaneously in one traversal . Abbreviation: front and back double pointers */
ListNode* removeNthFromEnd(ListNode* head, int n) {
/* First define a header node . Unified operation . The official name is dumb node , namely dummy*/
ListNode* dummy = new ListNode(0, head);
/* Define two pointers before and after one . The following pointer needs to go more than the previous pointer n+1 Time * Because after traversal . The back pointer finally points to null. You also need to ensure that the front pointer just points to the precursor of the deleted node .*/
/* Be careful * Has a higher priority than int, Will first combine variables ,listnode (*a) */
ListNode* front = dummy, * back = dummy;
for (int i = 1; i <= n + 1; i++)
back = back->next;
while (back) {
front = front->next;
back = back->next;
}
/* Delete */
front->next = front->next->next;
/* Don't go back head, The node pointed to by this pointer may have been deleted */
/* Benefits of defining header nodes . Unified operation . At the same time, it can accurately return the pointer corresponding to the first node . * That is, you don't need to maintain the head pointer alone . Choose between two operations */
return dummy->next;
}
};
边栏推荐
- Keyword inline (inline function) usage analysis [C language]
- RuntimeError: cuDNN error: CUDNN_STATUS_NOT_INITIALIZED
- js 变量作用域和函数的学习笔记
- NRF24L01 troubleshooting
- HCIP Day 12
- ES6语法总结--下篇(进阶篇 ES6~ES11)
- [Nodejs] 20. Koa2 onion ring model ----- code demonstration
- Detailed explanation of truncate usage
- Pat 1097 duplication on a linked list (25 points)
- How to add music playback function to Arduino project
猜你喜欢
JS变量类型以及常用类型转换
NRF24L01故障排查
ES6 grammar summary -- Part 2 (advanced part es6~es11)
MySQL时间、时区、自动填充0的问题
[Red Treasure Book Notes simplified version] Chapter 12 BOM
Stm32f1+bc20+mqtt+freertos system is connected to Alibaba cloud to transmit temperature and humidity and control LED lights
Intermediate use tutorial of postman [environment variables, test scripts, assertions, interface documents, etc.]
AMBA、AHB、APB、AXI的理解
[esp32 learning-2] esp32 address mapping
Cannot change version of project facet Dynamic Web Module to 2.3.
随机推荐
Gravure sans fil Bluetooth sur micro - ordinateur à puce unique
Classification, understanding and application of common methods of JS array
Detailed explanation of Union [C language]
The first simple case of GNN: Cora classification
ES6 grammar summary -- Part 2 (advanced part es6~es11)
(五)R语言入门生物信息学——ORF和序列分析
Cannot change version of project facet Dynamic Web Module to 2.3.
[Offer29] 排序的循环链表
MySQL replacement field part content
Pat 1097 duplication on a linked list (25 points)
Kaggle competition two Sigma connect: rental listing inquiries
(3) Introduction to bioinformatics of R language - function, data Frame, simple DNA reading and analysis
About using @controller in gateway
Mp3mini playback module Arduino < dfrobotdfplayermini H> function explanation
JS function promotion and declaration promotion of VaR variable
JS regular expression basic knowledge learning
VIM command line notes
记一次云服务器被密码爆破的经历——关小黑屋、改密码、改端口
Fashion-Gen: The Generative Fashion Dataset and Challenge 论文解读&数据集介绍
ES6语法总结--下篇(进阶篇 ES6~ES11)