当前位置:网站首页>36. Delete the penultimate node of the linked list
36. Delete the penultimate node of the linked list
2022-07-24 12:47:00 【Little happy】
The first 21 God
19. Delete the last of the linked list N Nodes
Medium difficulty 1475 Switch to English to receive dynamic feedback
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 ?
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]
Traverse
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode a = new ListNode(0);
a.next = head;
ListNode p = a;
int len = 0;
while(p.next!=null){
len++;
p = p.next;
}
int t = len - n+1;
p = a;
for(int i=0;i<len;i++){
if(i==t-1){
p.next = p.next.next;
}
p=p.next;
}
return a.next;
}
}
Speed pointer
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode a = new ListNode(0);
a.next = head;
ListNode p = a;
ListNode q = a;
for(int i=0;i<n;i++) p = p.next;
while(p.next!=null){
p = p.next;
q = q.next;
}
q.next = q.next.next;
return a.next;
}
}
Stack
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummy = new ListNode(0, head);
Deque<ListNode> stack = new LinkedList<ListNode>();
ListNode cur = dummy;
while (cur != null) {
stack.push(cur);
cur = cur.next;
}
for (int i = 0; i < n; ++i) {
stack.pop();
}
ListNode prev = stack.peek();
prev.next = prev.next.next;
ListNode ans = dummy.next;
return ans;
}
}
边栏推荐
- No routines, no traps, no advertisements | are you sure you don't need this free instant messaging software?
- Everything about native crash
- The price of domestic flagship mobile phones is nearly 6000, but they can't even beat iphone12. It's clear who users choose
- 【C语言】动态内存管理
- ASP. Net core deployment Manual: 1. Deployment Basics
- 基于matlab的语音处理
- Summary of recent interviews
- Cluster construction based on kubernetes v1.24.0 (III)
- What kind of experience is a monthly salary of 30000 yuan? Can we achieve this level as we media
- 高速成长的背后,华为云乌兰察布数据中心的绿色之道
猜你喜欢

Use abp Zero builds a third-party login module (III): web side development

The price of domestic flagship mobile phones is nearly 6000, but they can't even beat iphone12. It's clear who users choose

Nacos deployment
EfficientFormer:轻量化ViT Backbone

MobileViT:挑战MobileNet端侧霸主

Reserved instances & Savings Plans

基于matlab的语音处理

Support liuhaiping

使用TypeFace设置TextView的文字字体

C language course design -- hotel management system
随机推荐
Prototype inheritance
Snowflake algorithm (PHP)
It is difficult for Chinese consumers and industrial chains to leave apple, and iPhone has too much influence
向勒索病毒说不,是时候重塑数据保护策略
The setting float cannot float above the previous Div
Is it safe to contact the account manager online to open a fund account?
Leetcode's 302 weekly rematch
Industry insight | how to better build a data center? It and business should "go together"
Reserved instances & Savings Plans
使用Jenkins搭建CI服务器
leetcode第 302 场周赛复盘
Ape anthropology topic 5
基于matlab的声音识别
Summary of recent interviews
SSM在线校园相册管理平台
3. Realize snake and basic game interface
Reserved instances & Savings Plans
自己实现is_default_constructible
QWaitCondition 的正确使用方法
STM32 - Fundamentals of C language