当前位置:网站首页>leetcode 19. 删除链表的倒数第 N 个结点
leetcode 19. 删除链表的倒数第 N 个结点
2022-06-10 03:01:00 【C卷卷】
题目描述
https://leetcode.cn/problems/remove-nth-node-from-end-of-list/
思路
快慢指针 , 审题!!!
代码1
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} * ListNode(int x, ListNode *next) : val(x), next(next) {} * }; */
//注意审题 是倒数n个结点!!!
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
if(!head ||!head->next) {
return NULL;
}
ListNode *fast = head, *slow = head;
for(int i = 0; i < n; i ++) {
fast = fast -> next;// fast比slow先走n步
}
if(fast == NULL) {
// 删除头结点
return head->next;
}
while(fast -> next) {
fast = fast->next;
slow = slow->next;
}
slow ->next= slow->next->next;
return head;
}
};
感谢阅读
边栏推荐
- Wechat applet music playing code (playing mode, lyrics scrolling)
- Jupyter notebook configuring virtual environments
- TS 38.304
- Some problems of scanf formatting input
- Esp32 intrinsic function / variable cannot jump to definition
- 在子集合中第一个元素是由另一个数组插入的
- Anaconda modify file save path
- Numpy matrix operation
- How to hack user code gracefully
- Knight Moves
猜你喜欢
随机推荐
Promise introduction and Implementation
TiDB经验分享02
Microgrid digital twin 𞓜 intelligent era, deploy the integrated control platform of source network load and storage
Using the TMUX (Terminal Multiplexer) command
ModuleNotFoundError: No module named ‘pip‘
Modify pycharm cache file path
HCIE-Routing & Switching实验考试延期至2022年12月31日
Redis迭代查询详解及其使用:Scan命令、Sscan命令、Hscan命令、Zscan命令
1px problem
公众号留存操作时应该注意哪些问题?
Flutter dual end scanning download app
midway的使用教程
Analysis of the meaning of autocorrelation function / autocovariance function in different fields
^28js is single threaded
Robustness problem -- a work of Enlightenment
Using GDI to realize multi-channel video stream merging
Difference between generate for and for
SQL Server SQL statement create index
uniapp 开发app时获取唯一标识OAID,imei,ooid
获取省市区列表【项目 商城】







![College entrance examination [activities]](/img/da/b7e756a0a0298680142f62337f64b0.jpg)

