当前位置:网站首页>Sword finger offer II 021 Delete the penultimate node of the linked list
Sword finger offer II 021 Delete the penultimate node of the linked list
2022-06-10 00:50:00 【Small white yards fly up】
Summary
Double pointer to the right , The right pointer goes to the end , Handle the penultimate with the left pointer n Nodes
subject
Given a linked list , Delete the last of the linked list n Nodes , And return the head node of the list .

link :https://leetcode.cn/problems/SLwz0R/
Ideas
Finally, I entered the linked list stage .
Because it's a linked list , So we don't know the length of the linked list . This is a classic question , If it is toutie, go to the penultimate n Nodes , Then go through it first to get the length , Then traverse again or go back .
Of course , Here we still need to sacrifice our double pointer method . Starting from the chain header node , The right hand goes first , Walked a distance n after , The left pointer and the right pointer start at the same time . When the right pointer comes to the end , The next position of the left pointer is the penultimate position n Nodes .
in addition , The problem of linked list , A sentinel node is often used to point to the linked list head, It greatly facilitates our processing .
solution : The sentinel node + Double pointer
Code
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummy = new ListNode();
// The sentinel node can be used to easily handle the removed nodes head The situation of
dummy.next = head;
ListNode left = dummy;
ListNode right = dummy;
int count = 0;
// Right pointer walking n Step
while (right.next != null && count < n) {
right = right.next;
count++;
}
// Move left and right at the same time
while (right.next != null) {
right = right.next;
left = left.next;
}
// Remove node
left.next = left.next.next;
return dummy.next;
}
边栏推荐
- CloudCompare&PCL 主曲率、平均曲率以及高斯曲率计算
- Cloning of EBS r12.2
- Solution to the C language problem of force deduction self divisor
- OSPF experiment
- Code case - web version confession wall and file upload
- BGP协议实验
- BGP experiment
- Performance optimization scheme
- flutter pub get failed (66; Could not find a file named “pubspec.yaml“
- [HFCTF2020]BabyUpload
猜你喜欢
Solution to the C language problem of force deduction self divisor

wps合并单元格快捷键是什么

Interface control devaxpress WinForms -- new WXI skin grabs "fresh" view

The latest version of Alibaba development manual is released in 2022!!!
Palindromes of past real questions of test questions date [11th session] [provincial competition] [group B]

BGP实验

ospf总结

Pycharm 2022 permanently activated version download, valid for personal test

Cloud Mining & cloud mining chain: from order collaboration to procurement supply chain, make procurement supply chain interconnected

剑指 Offer II 016. 不含重复字符的最长子字符串
随机推荐
Cloning of EBS r12.2
内网渗透简洁笔记
2018年虚拟仪器竞赛作品分享-基于LabVIEW、百度地图、STM32单片机等的可重构蛇形机器人
Rhcsa day 3
Rip experiment
剑指 Offer II 016. 不含重复字符的最长子字符串
The binary tree is expanded into a linked list [the essence of tree processing -- how to handle each sub tree]
Solution to C language problem of adding two numbers by force deduction
内网渗透隧道
线性规划和对偶规划学习总结
[docker]mysql scheduled backup
Syntaxe des points d'interrogation doubles, syntaxe des barres verticales doubles et syntaxe des chaînes optionnelles pour l'attribution logique de l'espace JS
IDC fait autorité pour prédire que l'industrie manufacturière chinoise est sur le point de monter dans le nuage
CloudCompare&PCL 主曲率、平均曲率以及高斯曲率计算
Flutter ITMS-90338: Non-public API usage - Frameworks/webview_ flutter_ wkwebview. framework
js 逻辑空分配双问号语法 、双竖杠语法 与 可选链语法
JS logic empty allocation double question mark syntax, double vertical bar syntax and optional chain syntax
Score of sub series of previous test questions and [11th] [provincial competition] [group B]
1049大盗阿福
RHCSA第一天