当前位置:网站首页>Leetcode topic resolution remove nth node from end of list
Leetcode topic resolution remove nth node from end of list
2022-06-23 06:16:00 【ruochen】
Given a linked list, remove the nth node from the end of list and return its head.
For example,
Given linked list: 1->2->3->4->5, and n = 2.
After removing the second node from the end, the linked list becomes 1->2->3->5.
Note:
Given n will always be valid.
Try to do this in one pass.
In the title n It's legal. , You don't have to n Checked . With the idea of a ruler , The distance between the two pointers is n-1, The last one goes to the end of the table , Then the previous to n 了 .(p by second,q by first)
- The pointer p、q Point to the head of the list ;
- Move q, send p and q Bad n-1;
- Move at the same time p and q, send q To the end of the table ;
- Delete p.
public ListNode removeNthFromEnd(ListNode head, int n) {
if (head == null || head.next == null) {
return null;
}
ListNode first = head;
ListNode second = head;
for (int i = 0; i < n; i++) {
first = first.next;
if (first == null) {
return head.next;
}
}
while (first.next != null) {
first = first.next;
second = second.next;
}
second.next = second.next.next;
return head;
}边栏推荐
- About the error of installing PIP3 install chatterbot
- 【开源项目】excel导出lua配置表工具
- Difference between MySQL read committed and repeatability
- mysql读已提交和可重复度区别
- Centos7 deploy radius service -freeradius-3.0.13-15 EL7 integrating MySQL
- Visual studio debugging tips
- jvm-05. garbage collection
- WordPress aawp 3.16 cross site scripting
- jvm-06. Garbage collector
- Fraction to recursing decimal
猜你喜欢
![[cocos2d-x] screenshot sharing function](/img/fc/e3d7e5ba164638e2c48bc4a52a7f13.png)
[cocos2d-x] screenshot sharing function

Pyqt5 设置窗口左上角图标

jvm-04.对象的内存布局

Infotnews | which Postcard will you receive from the universe?

Microsoft interview question: creases in origami printing

Android handler memory leak kotlin memory leak handling

Summary of ant usage (I): using ant to automatically package apk

ant使用总结(一):使用ant自动打包apk

最优传输理论下对抗攻击可解释性

Redis sentry
随机推荐
How to batch produce QR codes that can be read online after scanning
如何为 Arduino IDE 安装添加库
jvm-04.对象的内存布局
(1) Basic learning - Common shortcut commands of vim editor
Global attribute lang attribute
[cocos2d-x] screenshot sharing function
SQL表名与函数名相同导致SQL语句错误。
机器学习3-岭回归,Lasso,变量选择技术
How to add libraries for Arduino ide installation
基于T5L1的小型PLC设计方案
【Cocos2d-x】自定义环形菜单
Pyinstaller sklearn报错的问题
【Cocos2d-x】可擦除的Layer:ErasableLayer
Network packet capturing tcpdump User Guide
Excel sheet column title for leetcode Title Resolution
Pyinstaller packaging pyttsx3 error
Pat class B 1014 C language
Wireshark TS | 视频 APP 无法播放问题
Microsoft interview question: creases in origami printing
Operating mongodb in node