当前位置:网站首页>18. Delete the penultimate node of the linked list
18. Delete the penultimate node of the linked list
2022-07-26 02:21:00 【linsa_ pursuer】
Given a linked list , Delete the last of the linked list n A node and returns the header pointer of the linked list
for example ,
The list given is : 1\to 2\to 3\to 4\to 51→2→3→4→5, n= 2n=2.
Deleted the penultimate of the linked list nn After nodes , Linked list becomes 1\to 2\to 3\to 51→2→3→5.
Data range : Chain length 0\le n \le 10000≤n≤1000, The value of any node in the linked list satisfies 0 \le val \le 1000≤val≤100
requirement : Spatial complexity O(1)O(1), Time complexity O(n)O(n)
remarks :
Title assurance nn It must be effective
Example 1
Input :
{1,2},2
Copy
Return value :
{2}
The code is as follows :
import lombok.ToString;
@ToString
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}public class Main {
public static void main(String[] args) throws Exception {
ListNode head = new ListNode(1);
ListNode headOne = new ListNode(2);
ListNode headTwo = new ListNode(3);
head.next = headOne;
headOne.next = headTwo;
System.out.println(head);
System.out.println(removeNthFromEnd(head, 3));
}
public static ListNode removeNthFromEnd (ListNode head, int n) {
ListNode first = head;
ListNode second = head;
for(int i = 0; i < n; i++) {
first = first.next;
}
// If n The value of is equal to the length of the linked list , Directly return to the linked list of U-turn nodes
if (first == null) {
return head.next;
}
while(first.next != null) { // Move both pointers at the same time
first = first.next;
second = second.next;
}
second.next = second.next.next;
return head;
}
}边栏推荐
- Basic usage of set, map, DOM classlist in ES6
- These practical security browser plug-ins improve your efficiency
- Design and driver transplantation of matrix keyboard circuit of Ti am335x industrial control module
- Data warehouse: Practice of hierarchical structure of data warehouse in banking industry
- What is JSX?
- U++ common type conversion and common forms and proxies of lambda
- I.MX6UL核心模块使用连载-nand flash读写测试 (三)
- 博云容器云、DevOps 平台斩获可信云“技术最佳实践奖”
- Ti AM335X工控模块矩阵键盘电路的设计与驱动移植
- 3. Upload the avatar to qiniu cloud and display it
猜你喜欢

项目管理:精益管理法

c# 单元测试
![[2020] [paper notes] growth of bi2te3/cofeb double-layer heterojunction by magnetron sputtering——](/img/5d/7d26e2d0d832c95e1cc011995ce774.png)
[2020] [paper notes] growth of bi2te3/cofeb double-layer heterojunction by magnetron sputtering——

I.MX6UL核心模块使用连载-nand flash读写测试 (三)

i. Mx6ull snvs power domain GPIO status hold verification

我来图书馆小程序一键签到和一键抢位置工具

(CVPR 2019) GSPN: Generative Shape Proposal Network for 3D Instance Segmentation in Point Cloud

TI AM335x工控模块网络跟文件系统NFS的实现

MySQL(4)

主键B+ Tree,二级索引B+ Tree及对应的查询过程分析
随机推荐
Illustration of the insertion process of b+ tree
【云原生】4.1 DevOps基础与实战
1. Mx6ul core module serial Ethernet test (VII)
Postman reported JSON serialization error
1. Mx6ul core module use serial RTC test (XII)
Error reporting caused by local warehouse
From a data incremental processing problem, we can see the consciousness gap of technicians
【PyQt5打包为exe】
1205 Lock wait timeout exceeded; Try restarting transaction processing
I.MX6UL核心模块使用连载-eMMC读写测试 (四)
[2020] [paper notes] growth of bi2te3/cofeb double-layer heterojunction by magnetron sputtering——
(CVPR 2019) GSPN: Generative Shape Proposal Network for 3D Instance Segmentation in Point Cloud
U++ common type conversion and common forms and proxies of lambda
1. Mx6ul core module uses serial EMMC read / write test (IV)
1. Mx6ul core module uses serial NAND FLASH read / write test (III)
Sqlyog data import and export graphic tutorial
Quick start of adding, deleting, modifying and checking business
ERROR: could not extract tar starting at offset 000000000000020980+9231072+2
1. Mx6ul core module serial WiFi test (VIII)
1. Mx6ul core module serial USB interface test (VI)