当前位置:网站首页>19. Delete the penultimate node of the linked list (linked list)
19. Delete the penultimate node of the linked list (linked list)
2022-04-23 10:15:00 【Popuessing's Jersey】
subject : I'll give you a list , Delete the last of the linked list n Nodes , And return the head node of the list .
Advanced : Can you try a scan implementation ?
public class ShanchulianbiaodedaoshudiNgejiedian {
public ListNode removeNthfromEnd(ListNode head,int n){
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode left = dummy;
ListNode right = dummy;
// Let the quick pointer go first n Step , Then let the fast and slow pointers go together ,
// Until the pointer goes to empty , At this time, the slow pointer points to the penultimate n Nodes ( But it's not convenient to delete )
// The deletion operation needs to find the previous node of the deleted object , So the fast pointer can go first n+1 Step , Then the node pointed by the slow pointer is the countdown n+1 There are nodes
for (int i = 0; i <=n ; i++) {
if(right.next!=null) {
right = right.next;
}
}
// Looking for the penultimate n+1 Nodes
while (right!=null){
left = left.next;
right = right.next;
}
// Delete the last n Nodes
left.next = left.next.next;
return dummy.next;
}
// Printout
static void print(ListNode listNode){
// Create a linked list node
while (listNode!=null){
if(listNode.next==null){
System.out.print(listNode.val);
}else {
System.out.print(listNode.val + "->");
}
listNode = listNode.next;
}
}
public static void main(String[] args) {
// Create the first node
ListNode node = new ListNode(1);
// Define mobile nodes
ListNode nextnode = node;
for (int i = 2; i <=6 ; i++) {
// Generate new nodes
ListNode nodex = new ListNode(i);
// The mobile node points to the new node ( Connect the new node
nextnode.next = nodex;
// Move the current node back
nextnode = nextnode.next;
}
// Linked list generation completed
// Print directly from the first node
print(node);
System.out.println();
ShanchulianbiaodedaoshudiNgejiedian shanchulianbiaodedaoshudiNgejiedian = new ShanchulianbiaodedaoshudiNgejiedian();
ListNode res = shanchulianbiaodedaoshudiNgejiedian.removeNthfromEnd(node,3);
print(res);
}
}
Output results :
1->2->3->4->5->6
1->2->3->5->6
Time complexity :O(n)
Spatial complexity :O(1)
版权声明
本文为[Popuessing's Jersey]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231011140971.html
边栏推荐
- 101. Symmetric Tree
- Yarn core parameter configuration
- 杰理之有时候定位到对应地址的函数不准确怎么办?【篇】
- CSP certification 202203-2 travel plan (multiple solutions)
- MapReduce压缩
- Sim Api User Guide(6)
- 二叉树的构建和遍历
- DBA common SQL statements (3) - cache, undo, index and wait events
- Using idea to develop Spark Program
- Realize data value through streaming data integration (3) - real-time continuous data collection
猜你喜欢
随机推荐
Custom login failure handling
解决方案架构师的小锦囊 - 架构图的 5 种类型
Question bank and answers of Shanghai safety officer C certificate examination in 2022
242、有效字母异位词(哈希表)
ansible 云计算 自动化
Sim Api User Guide(7)
转:毛姆:阅读是一座随身携带的避难所
59、螺旋矩阵(数组)
NEC infrared remote control coding description
实践六 Windows操作系统安全攻防
Function realization of printing page
域名和IP地址的联系
Ansible playbook syntax and format automate cloud computing
通过流式数据集成实现数据价值(3)- 实时持续数据收集
SQL调优系列文章之—SQL性能方法论
Examination questions and answers of the third batch (main person in charge) of Guangdong safety officer a certificate in 2022
2022茶艺师(初级)考试试题模拟考试平台操作
Sim Api User Guide(4)
DBA common SQL statements (1) - overview information
正大国际讲解道琼斯工业指数到底是什么?