当前位置:网站首页>19. delete the penultimate node of the linked list
19. delete the penultimate node of the linked list
2022-06-11 08:55:00 【Drag is me】
leetcode Force button to brush questions and punch in
subject :19. Delete the last of the linked list N Nodes
describe : I'll give you a list , Delete the last of the linked list n Nodes , And return the head node of the list .
Their thinking
1、 You can use arrays to store linked list nodes ;
2、 With a double pointer , See the code ;
Source code ##
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode *p = head;
ListNode *slow = head;
ListNode *fast = head;
for (int i = 0; i < n; ++i) {
fast = fast->next;
}
if (!fast) return slow->next;
while (fast->next) {
slow = slow->next;
fast = fast->next;
}
slow->next = slow->next->next;
return p;
}
};
边栏推荐
- 2130. 链表最大孪生和
- Codetop - sort odd ascending even descending linked list
- CodeTop - 排序奇升偶降链表
- K8s application (IV) - build a redis5 cluster (for direct external access)
- C language printing heart
- 欧洲家具EN 597-1 跟EN 597-2两个阻燃标准一样吗?
- PHP解决中文显示乱码
- Hibernate L2 cache
- File system check of the root filesystem failed
- 利用docker-compose搭建redis5集群
猜你喜欢

剑指 Offer 21. 调整数组顺序使奇数位于偶数前面

Which Apple devices support this system update? See if your old apple device supports the latest system

处理RAW格式的图像,需要什么软件?

leetcode - 739. 每日温度

Matlab学习8-图像处理之线性与非线性锐化滤波、非线性平滑滤波

MATLAB R2022a 安装教程

M1 chip guide: M1, M1 pro, M1 Max and M1 ultra

Sword finger offer 51 Reverse pair in array

Textview text size auto fit and textview margin removal

领导让我重写测试代码,我也要照办嘛?
随机推荐
GCC AVR (ATMEL studio+ AVR studio) how to define the structure array in the program memory (flash) space and read it
面試題 02.02. 返回倒數第 k 個節點
Installation (detailed illustration) and use of SVN
Matlab learning 8- linear and nonlinear sharpening filtering and nonlinear smoothing filtering of image processing
Sword finger offer 62 The last remaining number in the circle
682. 棒球比赛
利用docker-compose搭建redis5集群
Sword finger offer 06 Print linked list from end to end
c的printf相关
完整的ES6面试题
Codeworks round 680 div2
MySQL upgrade
窗帘做EN 1101易燃性测试过程是怎么样的?
剑指 Offer 06. 从尾到头打印链表
BS 7176 fire resistance test for upholstered furniture
剑指 Offer 21. 调整数组顺序使奇数位于偶数前面
Codeworks round 723 (Div. 2)
Cron expressions in scheduled tasks
Standardized compilation knowledge
M1 chip guide: M1, M1 pro, M1 Max and M1 ultra