双指针法

双指针法主要是最开始有两个指针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. vulnhub - hackme2

    vulnhub - hackme2 信息收集 还是跟1一样,目录扫描之类的没啥利用点,sql注入先打一遍 SQL注入 sqlmap -u 'http://192.168.157.163/welcome ...

  2. FSCTF 2023(公开赛道)WP

    FSCTF 2023 ID:Mar10 Rank:6 总结:下次看到不正常报错一定重新安装一遍工具~~ web 源码!启动! 就在源码注释里 <!-- 师傅们,欢迎来到CTF的世界~ NSSCT ...

  3. 开源GTKSystem.Windows.Forms,在这里更新预告

    开源GTKSystem.Windows.Forms,在这里更新预告 gitee码云开源地址:https://gitee.com/easywebfactory/gtksystem-windows-for ...

  4. Mysql生成实体类

    -- 查询数据表结构 SELECT CONCAT('"e.',SUBSTRING(COLUMN_NAME,1),',"+'),COLUMN_NAME,',',COLUMN_TYPE ...

  5. dubbo面试题及答案

    Dubbo是什么? Dubbo是阿里巴巴开源的基于 Java 的高性能 RPC 分布式服务框架,现已成为 Apache 基金会孵化项目. 面试官问你如果这个都不清楚,那下面的就没必要问了. 官网:ht ...

  6. 1.1 第一个hello程序

    还记得在每一个编程平台上的第一个程序都是hello world,现在就以这个程序为载体,先浅聊一下计算机系统吧. 1.预处理阶段,预处理器cpp根据字符#开头的命令修改原始的程序,并把头文件里的内容直 ...

  7. leetcode简单(数组,字符串,链表):[66, 67, 70, 83, 121, 141, 160, 169, ,206, 338]

    目录 66. 加一 67. 二进制求和 70. 爬楼梯 83. 删除排序链表中的重复元素 121. 买卖股票的最佳时机 141. 环形链表 160. 相交链表 169. 多数元素 206. 反转链表 ...

  8. webpack4.15.1 学习笔记(五) — 生产环境构建

    目录 生产环境构建 指定环境 生产环境构建 development和production的构建目标差异很大.dev中,需要具有实时重新加载或HMR能力的 source map 和 server.而在p ...

  9. Pycharm中开发vue element项目时eslint的安装和使用

    在PyCharm中使用ESLint对Element UI进行语法检查和代码风格检查的配置步骤如下: 确保你的项目已经配置了ESLint并且可以正常运行.如果尚未安装ESLint,请先使用npm(或者你 ...

  10. EasyDesktop 浏览器书签管理从未如此简单

    作为一名软件开发从业人员, 每天80%的时间都在与浏览器打交道, 一半的时间在用浏览器开发调试, 另一半时间则是在互联网上搜寻各种知识和资源. 为此, 我的浏览器书签栏存储和很多非常棒的链接, 多到2 ...