当前位置:网站首页>[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;
}
};
边栏推荐
- A possible cause and solution of "stuck" main thread of RT thread
- 嵌入式启动流程
- [esp32 learning-2] esp32 address mapping
- Priority inversion and deadlock
- ES6语法总结--下篇(进阶篇 ES6~ES11)
- MySQL replacement field part content
- [Red Treasure Book Notes simplified version] Chapter 12 BOM
- Minio文件下载问题——inputstream:closed
- Symbolic representation of functions in deep learning papers
- Rough analysis of map file
猜你喜欢

Générateur d'identification distribué basé sur redis

History object

Gravure sans fil Bluetooth sur micro - ordinateur à puce unique

Redis based distributed locks and ultra detailed improvement ideas

Priority inversion and deadlock

STM32 how to locate the code segment that causes hard fault
![Several declarations about pointers [C language]](/img/9b/ace0abbd1956123a945a98680b1e86.png)
Several declarations about pointers [C language]

ARM PC=PC+8 最便于理解的阐述

A possible cause and solution of "stuck" main thread of RT thread

Pytorch four commonly used optimizer tests
随机推荐
C language, log print file name, function name, line number, date and time
. elf . map . list . Hex file
@The difference between Autowired and @resource
Mysqldump error1066 error solution
Basic operations of databases and tables ----- view data tables
AMBA、AHB、APB、AXI的理解
ES6 grammar summary -- Part I (basic)
ORA-02030: can only select from fixed tables/views
Kconfig Kbuild
Get the position of the nth occurrence of the string
(4) Data visualization of R language -- matrix chart, histogram, pie chart, scatter chart, linear regression and strip chart
Cannot change version of project facet Dynamic Web Module to 2.3.
js题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
NRF24L01故障排查
[esp32 learning-2] esp32 address mapping
MP3mini播放模块arduino<DFRobotDFPlayerMini.h>函数详解
Esp8266 connects to bafayun (TCP maker cloud) through Arduino IED
Common DOS commands
[offer78]合并多个有序链表
Page performance optimization of video scene