当前位置:网站首页>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;
}边栏推荐
- Global attribute lang attribute
- Pat class B 1019 C language
- Runc symbolic link mount and container escape vulnerability alert (cve-2021-30465)
- Paper notes: multi label learning lsml
- Pat class B 1026 program running time
- Pat class B 1016 C language
- mongodb 4. X binding multiple IP startup errors
- 学习太极创客 — ESP8226 (十一)用 WiFiManager 库配网
- Pat class B 1012 C language
- [DaVinci developer topic] -41-app how SWC reads and writes NVM block data
猜你喜欢

ant使用总结(一):使用ant自动打包apk
![[cocos2d-x] custom ring menu](/img/fd/c18c39ae738f6c1d2b76b6c54dc654.png)
[cocos2d-x] custom ring menu

Kotlin Android simple activity jump, simple combination of handler and thread

Ant Usage Summary (II): description of related commands

Wireshark TS | video app cannot play

mongodb 4. X binding multiple IP startup errors

微软面试题:打印折纸的折痕

Visual Studio调试技巧

Pyqt5 设置窗口左上角图标
![[cocos2d-x] erasable layer:erasablelayer](/img/6e/1ee750854dfbe6a0260ca12a4a5680.png)
[cocos2d-x] erasable layer:erasablelayer
随机推荐
WordPress contact form entries cross cross site scripting attack
Pat class B 1026 program running time
Tcp/ip explanation (version 2) notes / 3 link layer / 3.4 bridge and switch
Basic calculator II for leetcode topic analysis
微软面试题:打印折纸的折痕
Pat class B 1009 C language
Excel sheet column title for leetcode Title Resolution
Efficient office of fintech (I): automatic generation of trust plan specification
【Cocos2d-x】自定义环形菜单
Pat class B 1025 reverse linked list
matplotlib savefig多个图片叠加问题
jvm-03.jvm内存模型
Pat class B 1022 d-ary a+b
Palindrome number for leetcode topic analysis
Pat class B 1020 Moon Cake
Network packet capturing tcpdump User Guide
Extend your kubernetes API using the aggregation API
[vivado] xilinxcedstore introduction
11、 Realization of textile fabric off shelf function
(1) Basic learning - Common shortcut commands of vim editor