当前位置:网站首页>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;
}
}边栏推荐
- Monitoring of debezium synchronization debezium
- 1. Mx6ul core module serial WiFi test (VIII)
- I.MX6UL核心模块使用连载-RS485测试 (十)
- LeetCode_动态规划_中等_264.丑数 II
- JS add random pixel noise background to the page
- 3. Upload the avatar to qiniu cloud and display it
- prometheus+blackbox-exporter+grafana 监控服务器端口及url地址
- [2019] [paper notes] tunable THz broadband absorption based on metamaterials——
- [C language brush leetcode] 146. LRU cache (m)
- Li Kou daily question - day 39 -67. Binary sum
猜你喜欢

Illustration of the insertion process of b+ tree

Business Intelligence BI full analysis, explore the essence and development trend of Bi

Primary key b+ tree, secondary index b+ tree and corresponding query process analysis

Adruino basic experimental learning (I)
![[paper reading] coat: CO scale conv attentional image transformers](/img/d4/13ac8cdce07999d4fd51aa23173190.png)
[paper reading] coat: CO scale conv attentional image transformers

一款可插拔的AM335X工控模块板载wifi模块

1. Mx6ul core module use serial -rs485 test (x)

Ti AM335X工控模块使用beaglebone(bbb)的Debian系统

1. Mx6ul core module serial WiFi test (VIII)

博云容器云、DevOps 平台斩获可信云“技术最佳实践奖”
随机推荐
2022.7.25-----leetcode.919
The slow loading of the first entry page of vite local operation
Li Kou daily question - day 39 -67. Binary sum
ggplot2学习总结
图解B+树的插入过程
1. Mx6ul core module use serial -rs485 test (x)
Wechat applet decryption and unpacking to obtain source code tutorial
020-024 polymorphism review
租户问题。
1. Mx6ul core module uses serial NAND FLASH read / write test (III)
JS add random pixel noise background to the page
[C language brush leetcode] 1462. curriculum IV (m)
Binary logs in MySQL
Basic usage of set, map, DOM classlist in ES6
[red team] att & CK - using bits services to achieve persistence
U++ common type conversion and common forms and proxies of lambda
2022.7.25-----leetcode.919
I.MX6UL核心模块使用连载-USB接口测试 (六)
Ti AM335X工控模块使用beaglebone(bbb)的Debian系统
numpy.sort