当前位置:网站首页>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;
}边栏推荐
- Day_13 傳智健康項目-第13章
- Ant Usage Summary (II): description of related commands
- [vivado] xilinxcedstore introduction
- Leetcode topic resolution integer to Roman
- [cocos2d-x] erasable layer:erasablelayer
- Machine learning 3-ridge regression, Lasso, variable selection technique
- [cocos2d-x] custom ring menu
- Remove duplicates from sorted list II of leetcode topic resolution
- Perfect squares for leetcode topic analysis
- 金融科技之高效办公(一):自动生成信托计划说明书
猜你喜欢

Day_11 传智健康项目-图形报表、POI报表

Infotnews | which Postcard will you receive from the universe?

mysql读已提交和可重复度区别

Day_12 传智健康项目-JasperReports

【开源项目】excel导出lua配置表工具

Machine learning 3-ridge regression, Lasso, variable selection technique

【Leetcode】431. Encode N-ary Tree to Binary Tree(困难)

Design scheme of Small PLC based on t5l1

ant使用总结(三):批量打包apk
![[cocos2d-x] custom ring menu](/img/fd/c18c39ae738f6c1d2b76b6c54dc654.png)
[cocos2d-x] custom ring menu
随机推荐
Ant Usage Summary (III): batch packaging apk
Global attribute lang attribute
Difference between MySQL read committed and repeatability
Pat class B 1025 reverse linked list
Leetcode topic analysis: factorial training zeroes
Addressing and addressing units
Memory analysis and memory leak detection
Kotlin interface
【开源项目】excel导出lua配置表工具
最优传输理论下对抗攻击可解释性
ant使用总结(三):批量打包apk
jvm-05.垃圾回收
Pat class B 1026 program running time
Introduction to JVM principle
Visual Studio调试技巧
[database backup] complete the backup of MySQL database through scheduled tasks
Vant web app calendar component performance optimization calendar add min date the minimum date page loads slowly
Pat class B 1021 digit statistics
Pat class B 1019 C language
【Vivado那些事儿】XilinxCEDStore介绍