当前位置:网站首页>leetcode 19. Delete the penultimate node of the linked list
leetcode 19. Delete the penultimate node of the linked list
2022-06-27 16:59:00 【chenyson】
difficulty : secondary
The frequency of :75
problem : I'll give you a list , Delete the last of the linked list n Nodes , And return the head node of the list
** Their thinking :** Double pointer , Just navigate to the node to be deleted and the previous node , You can delete
Be careful :
- Last but not least n Nodes , You can use a tail Pointer first n Step
- then tail and cur Went together , until tail Get to the last step
- This is the time cur Is the node to be deleted
- pre To locate the node before the node to be deleted
- Because there may only be one node , It will be empty after deletion , This situation needs to be written out in advance
Code :
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
// The first node has active , So you need a virtual head node
ListNode dummyhead=new ListNode(0);
dummyhead.next=head;
// Double pointer , Fixed distance pointer moves together
ListNode tail=head,pre=dummyhead,cur=head;
// When there is only one node , Delete must be for null
if(head.next==null) return null;
// Last but not least n individual , Last but not least n Moved to the last one n-1 individual
for(int i=0;i<n-1;i++){
tail=tail.next;
}
// The two pointers move together , When tail When you reach the last node , The location you just reached is the node you want to delete
while(tail.next!=null){
//pre Is used to mark the previous node of the deleted node
pre=cur;
cur=cur.next;
tail=tail.next;
}
// Delete cur node
pre.next=cur.next;
return dummyhead.next;
}
}
边栏推荐
- C système de gestion de la charge de travail des enseignants en langues
- Sliding window + monotone queue concept and example (p1886 Logu)
- Raspberry pie preliminary use
- How to improve it electronic equipment performance management
- Delete duplicate elements in the sorting linked list
- Oracle概念二
- Autodesk Navisworks 2022软件安装包下载及安装教程
- QT5 之信号与槽机制(演示控件自带的信号与槽函数关联)
- LeetCode每日一练(两数之和)
- Defiato is an innovation that combines user-friendly features of a centralized platform with defi services
猜你喜欢
一个机器人位于一个 m x n 网格的左上角 。机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角。问总共有多少条不同的路径?【LeetCodeHot100】
IDE Eval reset unlimited trial reset
#yyds干货盘点# 解决剑指offer:二叉树中和为某一值的路径(三)
Determine the maximum number of specific words in a string
A large number of missing anchor text
Handling method of occasional error reporting on overseas equipment
d3dx9_ How to repair 38.dll? d3dx9_ 38. How to download a missing DLL?
leetcode 200. Number of islands
字节跳动埋点数据流建设与治理实践
10分钟掌握mysql的安装步骤
随机推荐
Event listening mechanism
一个机器人位于一个 m x n 网格的左上角 。机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角。问总共有多少条不同的路径?【LeetCodeHot100】
Impressive questions
QT5 之信号与槽机制(演示控件自带的信号与槽函数关联)
[fxcg] today's market analysis
Determine the maximum number of specific words in a string
Smart wind power | Tupu software digital twin wind turbine equipment, 3D visual intelligent operation and maintenance
QT5.5.1桌面版安装配置过程中的疑难杂症处理(配置ARM编译套件)
Julia constructs diagonal matrix
Huawei cloud devcloud launched four new capabilities, setting two domestic firsts
leetcode 200. Number of islands
[the way of programmer training] - 3 Character count statistics
【多线程】线程通信调度、等待集 wait() 、notify()
深耕数字化,引领云原生,服务更多开发者
What do fast fashion brands care more about?
鴻蒙發力!HDD杭州站·線下沙龍邀您共建生態
07. Express routing
C système de gestion de la charge de travail des enseignants en langues
华为云DevCloud重磅发布四大新能力,创下国内两项第一
The time of localdatetime type (2019-11-19t15:16:17) is queried with the time range of Oracle