当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

ROS 笔记(07)— 客户端 Client 和服务端 Server 的实现

86. (cesium chapter) cesium overlay surface receiving shadow effect (gltf model)

2022.2.11

天才制造者:獨行俠、科技巨頭和AI|深度學習崛起十年

Differences between TCP and UDP

How MySQL deletes all redundant duplicate data

UWB超高精度定位系统架构图

pycharm 导包错误没有警告

Final review of brain and cognitive science

DBeaver 安装及配置离线驱动
随机推荐
1.16 learning summary
Multipass中文文档-提高挂载性能
基础查询
Basic query
Zhongshanshan: engineers after being blasted will take off | ONEFLOW u
Is education important or ability important in software testing
1.18 learning summary
[H5 development] 01 take you to experience H5 development from a simple page ~ the whole page implementation process from static page to interface adjustment manual teaching
Pycharm package import error without warning
Image translation /gan:unsupervised image-to-image translation with self attention networks
1.19 learning summary
ROS 笔记(07)— 客户端 Client 和服务端 Server 的实现
超高精度定位系统中的UWB是什么
Difference between return and yield
202.2.9
【Latex】错误类型总结(持更)
广和通联合安提国际为基于英伟达 Jetson Xavier NX的AI边缘计算平台带来5G R16强大性能
微信小程序保存圖片的方法
Datetime data type - min() get the earliest date and date_ Range() creates a date range, timestamp() creates a timestamp, and tz() changes the time zone
图像翻译/GAN:Unsupervised Image-to-Image Translation with Self-Attention Networks基于自我注意网络的无监督图像到图像的翻译