双指针法

双指针法主要是最开始有两个指针fast,slow都指向链表的虚拟头节点dummy,然后快指针先移动,这里需要先向后移动n+1位(因为你最终是要找到目标节点的前一个节点),然后slow和fast节点就开始同时移动,直至fast指向链表的最后一个节点的下一个指向null,此时slow节点就指向了链表目标节点的前一个节点

class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummy=new ListNode(0);
dummy.next=head;
ListNode fast=dummy;
ListNode slow=dummy;
int count=n+1;
while(count-->0){
fast=fast.next;
}
while(fast!=null){
slow=slow.next;
fast=fast.next;
}
slow.next=slow.next.next;
return dummy.next;
}
}

普通法

class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
int length=0;
ListNode get_length=head;
while(get_length!=null){
length++;
get_length=get_length.next;
} ListNode dummy=new ListNode(0);
dummy.next=head;
ListNode temp=dummy;
int count=length-n;
while(count-->0){
temp=temp.next;
}
temp.next=temp.next.next;
return dummy.next;
}
}

删除链表倒数第N个节点(19)的更多相关文章

  1. [leetcode]19. Remove Nth Node From End of List删除链表倒数第N个节点

    Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...

  2. Leetcode算法系列(链表)之删除链表倒数第N个节点

    Leetcode算法系列(链表)之删除链表倒数第N个节点 难度:中等给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点.示例:给定一个链表: 1->2->3->4-&g ...

  3. 19。删除链表倒数第N个节点

    class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next# 这道题还是很简单的,我们只 ...

  4. 删除链表倒数第n个节点

    题目: 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第二个节点后,链 ...

  5. LeetCode 删除链表倒数第N个节点

    基本思路 定义两个指示指针a b 让a先行移动n+1个位置 若a指向了NULL的位置,则删除的是头节点(由于走过了n+1个节点刚好指在尾部的NULL上) 否则让b与a一起移动直至a->next, ...

  6. [Leetcode] remove nth node from the end of list 删除链表倒数第n各节点

    Given a linked list, remove the n th node from the end of list and return its head. For example, Giv ...

  7. 删除单链表倒数第n个节点

    基本问题 如何删除单链表中的倒数第n个节点? 常规解法 先遍历一遍单链表,计算出单链表的长度,然后,从单链表头部删除指定的节点. 代码实现 /** * * Description: 删除单链表倒数第n ...

  8. leetcode 19.删除链表的第n个节点

    删除链表的第n个节点 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第 ...

  9. [LeetCode] 19. Remove Nth Node From End of List 移除链表倒数第N个节点

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  10. [LeetCode] Remove Nth Node From End of List 移除链表倒数第N个节点

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

随机推荐

  1. hadoop集群配置文件与功能对应解析

    以三个节点的集群为例: 总括: nodemanager ,datanode  --> slavesresourcemanager ---------->    yarn namenode ...

  2. 【ClickHouse】0:clickhouse学习4之表相关操作

    Clickhouse对表操作分为四大类:增删查改(INSERT,DROP,SELECT,ALTER). 增,删,查比较简单,改最复杂.那具体有哪些改的操作呢?如下清单: ALTER ALTER TAB ...

  3. sqlite相关

    前言 本文记录一些sqlite相关笔记,随时更新. 正文 时间函数 datetime() -- 当前时间 2022-03-24 17:32:43 select datetime('now'); --2 ...

  4. 深入解析 Vue Router:构建单页面应用的利器

    Vue.js 是一个渐进式 JavaScript 框架,常用于构建用户界面.随着应用的复杂度增加,路由(Routing)变得越来越重要,这就是 Vue Router 的用武之地.Vue Router ...

  5. 基于 Impala 的高性能数仓建设实践之虚拟数仓

    导读:本文主要介绍网易数帆 NDH 在 Impala 上实现的虚拟数仓特性,包括资源分组.水平扩展.混合分组和分时复用等功能,可以灵活配置集群资源.均衡节点负载.提高查询并发,并充分利用节点资源. 接 ...

  6. tp 模型hasOne、hasMany、belongsTo详解

    首先,这3个的大致中文意思:hasOne:有一个,加上主谓语应该是 ,A 有一个 BhasMany:有很多,A 有很多 BbelongsTo:属于, A 属于 B这里我们准备3张表来理解他们的关系:u ...

  7. workman的工作流程

    workerman有两种进程模型1.基本的master worker模型2.master gateway worker模型 master worker模型工作流程及进程间关系如下: master wo ...

  8. Java 中的一些知识点

    Java 中的一些知识点 Java 中的知识点 与C++相关 toString方法 super 与C++相关[了解的不是很多] 在Java程序中:一个方法以 ; 结尾,并且修饰符列表中有 native ...

  9. NAS使用

    openwrt下的samba设置 - 百度文库 (baidu.com) openwrt下 samba设置 (wjhsh.net) opkg updateopkg install shadow-user ...

  10. springboot简单正确的使用构造函数注入

    一个一个写构造函数太麻烦了,而且代码会显得非常多,这里我们可以采用lombok快捷注入 但是我们并不是所有的成员变量都需要进行注入,所以使用 @RequiredArgsConstrucotr 需要构造 ...