当前位置:网站首页>LeetCode 19. Delete the penultimate node of the linked list
LeetCode 19. Delete the penultimate node of the linked list
2022-06-26 04:58:00 【Grilled little fat sheep with charcoal...】
Title Description : I'll give you a list , Delete the last of the linked list n Nodes , And return the head node of the list .
Example 1:
Input :head = [1,2,3,4,5], n = 2
Output :[1,2,3,5]
Example 2:
Input :head = [1], n = 1
Output :[]
Example 3:
Input :head = [1,2], n = 1
Output :[1]
Thinking analysis :
Classic application of pointer , If you want to delete the penultimate n Nodes , Give Way fast Move n Step , And then let fast and slow Move at the same time , until fast Point to the end of the list . Delete slow The node you point to is OK .
First of all, I recommend you to use virtual head node , This facilitates the logic of deleting the actual header node .
Definition fast Pointers and slow The pointer , The initial value is virtual head node , Pictured :

fast First of all, let's go n + 1 Step , Why n+1 Well , Because that's the only way to move at the same time slow To point to the previous node of the deleted node ( Easy to delete ), Pictured :

fast and slow Move at the same time , until fast Point to the end , Pictured :

Delete slow Point to the next node , Pictured :

Code implementation mode 1 :
public static ListNode removeNthFromEnd(ListNode head, int n) {
// Create a virtual header node
ListNode dummyNode = new ListNode(-1);
dummyNode.next = head;
// Define speed pointer
ListNode fast = dummyNode, slow = dummyNode;
// fast Go ahead n+1 Step , such slow Point to the previous node of the node to be deleted
while (n >= 0){
fast = fast.next;
n--;
}
// fast,slow Simultaneous backward transfer
while (fast != null){
slow = slow.next;
fast = fast.next;
}
slow.next = slow.next.next;
return dummyNode.next;
}
Code implementation mode 2 :
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode slow = dummy;
ListNode fast = dummy;
while (n > 0) {
fast = fast.next;
n--;
}
// Node to be deleted slow Last node of
ListNode prev = null;
while (fast != null) {
prev = slow;
slow = slow.next;
fast = fast.next;
}
// Of the previous node next Pointer bypass Node to be deleted slow Direct to slow The next node of
prev.next = slow.next;
return dummy.next;
}
边栏推荐
猜你喜欢

Multipass Chinese document - setup driver

记录一次循环引用的问题

PowerShell runtime system IO exceptions

6.1 - 6.2 公钥密码学简介

Statsmodels Library -- linear regression model

UWB ultra high precision positioning system architecture

UWB超高精度定位系统原理图

ModuleNotFoundError: No module named ‘numpy‘

Rdkit chemical formula molecular formula search

How MySQL deletes all redundant duplicate data
随机推荐
How MySQL deletes all redundant duplicate data
【Latex】错误类型总结(持更)
问题随记 —— pip 换源
Dbeaver installation and configuration of offline driver
pycharm 导包错误没有警告
PowerShell runtime system IO exceptions
Datetime data type ---now() gets the current time, datetime() creation date, performs mathematical operations, and to_ Datetime() converts to date type and extracts various parts of date
Wechat applet exits the applet (navigator and api--wx.exitminiprogram)
Zhongshanshan: engineers after being blasted will take off | ONEFLOW u
YOLOV5训练结果的解释
[H5 development] 03- take you hand in hand to improve H5 development - single submission vs batch submission with a common interface
Introduction to classification data cotegory and properties and methods of common APIs
Sort query
微信小程序保存图片的方法
A ZABBIX self discovery script (shell Basics)
Zuul implements dynamic routing
Solution to back-off restarting failed container
PSIM software learning ---08 call of C program block
Using Matplotlib to add an external image at the canvas level
Multipass Chinese document - remote use of multipass