当前位置:网站首页>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;
}
}边栏推荐
- Activiti workflow gateway
- I.MX6UL核心模块使用连载-WIFI测试 (八)
- 1205 Lock wait timeout exceeded; Try restarting transaction processing
- 租户问题。
- I.MX6UL核心模块使用连载-eMMC读写测试 (四)
- 还在用==0 null equal 判断空值吗,对isEmpty 和 isBlank有多少了解呢
- (CVPR 2019) GSPN: Generative Shape Proposal Network for 3D Instance Segmentation in Point Cloud
- [xxl-job] xxl-job learning
- Acwing 379. hide and seek problem solution (minimum path non repeating point coverage)
- Quick start of adding, deleting, modifying and checking business
猜你喜欢
![[C]详解语言文件操作](/img/12/4affa1d3fb3e4ee126e1c1e3872d9b.png)
[C]详解语言文件操作

I.MX6UL核心模块使用连载-查看系统信息 (二)

1. Mx6ul core module uses serial NAND FLASH read / write test (III)

还在用==0 null equal 判断空值吗,对isEmpty 和 isBlank有多少了解呢

I.MX6UL核心模块使用连载-TF卡读写测试 (五)

Postman reported JSON serialization error

Handling process of the problem that the virtual machine's intranet communication Ping fails

U++ common type conversion and common forms and proxies of lambda

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

I.MX6UL核心模块使用连载-Iot-6ULX核心模块简要介绍 (一)
随机推荐
1. Mx6ul core module serial use - touch screen calibration (IX)
博云容器云、DevOps 平台斩获可信云“技术最佳实践奖”
U++ common type conversion and common forms and proxies of lambda
Dqn pytoch example
1. Mx6ul core module uses serial can and buzzer test (XI)
EAM系统能帮助企业做什么?
IDEA运行web项目出现乱码问题有效解决(附详细步骤)
What does the Red Cross in the SQL editor mean (toad and waterdrop have been encountered...)
[C language brush leetcode] 1604. Warn people who use the same employee card more than or equal to three times within an hour (m)
I.MX6UL核心模块使用连载-RTC测试 (十二)
力扣148:排序链表
Be careful about bitmap, the "memory Assassin"~
uni-app跨域配置
U++ print information to screen and log
What can EAM system help enterprises do?
I came to the library applet check-in process analysis
墨天轮高分技术文档分享——数据库安全篇(共48个)
[2019] [paper notes] tunable THz broadband absorption based on metamaterials——
prometheus+blackbox-exporter+grafana 监控服务器端口及url地址
These practical security browser plug-ins improve your efficiency