当前位置:网站首页>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;
}
}
边栏推荐
- 【多线程】线程通信调度、等待集 wait() 、notify()
- Four characteristics of transactions
- Oracle概念二
- P4251 [scoi2015] small convex play matrix (still a little confused)
- Handwritten promise series - all
- Ti Click: quickly set up tidb online laboratory through browser | ti- team interview can be conducted immediately
- Hongmeng makes efforts! HDD Hangzhou station · offline salon invites you to build ecology
- d3dx9_ Where is 35.dll? d3dx9_ Where can I download 35.dll
- Detailed explanation of various GPIO input and output modes (push-pull, open drain, quasi bidirectional port)
- The time of localdatetime type (2019-11-19t15:16:17) is queried with the time range of Oracle
猜你喜欢

Unity 阴影——阴影平坠(Shadow pancaking)

Oracle概念二

Yyds dry inventory solution sword finger offer: a path with a certain value in the binary tree (3)

Practice of constructing ten billion relationship knowledge map based on Nebula graph

What is the level 3 password complexity of ISO? How often is it replaced?

About MySQL: the phenomenon and background of the problem

一个机器人位于一个 m x n 网格的左上角 。机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角。问总共有多少条不同的路径?【LeetCodeHot100】

树莓派初步使用

Ping An technology's practice of migrating from Oracle to ubisql
![[the way of programmer training] - 3 Character count statistics](/img/0c/e506571a4b22edc7c02a346909eeab.jpg)
[the way of programmer training] - 3 Character count statistics
随机推荐
事件监听机制
Why should string be designed to be immutable?
如何提升IT电子设备效能管理
leetcode 200. Number of islands
Leetcode 5. Longest Palindromic Substring
What do fast fashion brands care more about?
About how vs2019c # establishes the login interface, the user name and password entered must match the records in the access database
After the mobile phone, it was reported that Samsung also cut the output of TV and other home appliance product lines
ROS "topic" programming implementation
Yyds dry inventory solution sword finger offer: a path with a certain value in the binary tree (3)
Realize simple three-D cube automatic rotation
[multithreading] thread communication scheduling, waiting set wait(), notify()
QT5 之信号与槽机制(信号与槽的基本介绍)
Determine the maximum number of specific words in a string
Bit.Store:熊市漫漫,稳定Staking产品或成主旋律
郎酒两大王牌产品成都联动共振,持续带动光瓶酒消费浪潮
数组表示若干个区间的集合,请你合并所有重叠的区间,并返回 一个不重叠的区间数组,该数组需恰好覆盖输入中的所有区间 。【LeetCodeHot100】
Performance problems caused by redis cache invalidation and competitive loading
软件测试-测试的概念,单元测试的详细介绍,如何设计测试用例
Handling of difficult and miscellaneous problems during the installation and configuration of qt5.5.1 desktop version (configuring arm compilation Kit)