双指针法

双指针法主要是最开始有两个指针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. GCC8 编译优化 BUG 导致的内存泄漏

    1. 背景 1.1. 接手老系统 最近我们又接手了一套老系统,老系统的迭代效率和稳定性较差,我们打算做重构改造,但重构周期较长,在改造完成之前还有大量的需求迭代.因此我们打算先从稳定性和迭代效率出发做 ...

  2. jQuery -- 手稿

  3. Window版 MySQL可视化工具 Navicat 面安装免激活绿色版

    网盘地址 链接:https://pan.baidu.com/s/1T0WyhGAFEt28GaU4wXhfrg 提取码:z4ww navicat15破解版 链接:https://pan.baidu.c ...

  4. Spring(注解方式)简单入门

    环境准备 maven jdk Spring Eclipse 项目创建 pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0 ...

  5. 敏捷开发(Scrum)

    ​ 一.敏捷的背景与动机 1.1 软件危机及软件工程的出现 速度是企业竞争致胜的关键因素,软件项目的最大挑战在于,一方面要应付变动中的需求,一方面要在紧缩的时程内完成项目,传统的软件工程难以满足这些要 ...

  6. 10.2 web服务器

    Web客户端和服务器之间的交互用的是一个基于文本的应用级协议,叫做HTTP(Hypertext Transfer Protocol,超文本传输协议).HTTP是一个简单的协议.一个Web客户端(即浏览 ...

  7. [oeasy]python0124_Code_page_437_IBM_5150_点阵式字形码_显示器效果

    字符显示器 回忆上次内容 简体和繁体的汉字 字符数量都超级大 感谢王选和陈堃銶等前辈发明了激光照排技术 中文排版从此使用上了gb2312编码   ​   添加图片注释,不超过 140 字(可选)   ...

  8. oeasy教您玩转linux010204-figlet

    我们来回顾一下 上一部分我们都讲了什么? 用 apt 查询并下载了 linuxlogo 用字符画出了 linux 发行版的 logo 还查了手册,通过改参数控制输出信息 我们还能玩点什么呢? 这个实验 ...

  9. windows生成苹果私钥证书p12证书和profile文件的方法

    hbuilderx出现已经有差不多10年时间了,现在越来越多的企业,开始使用跨平台性更优秀的uniapp来开发ios app. 开发ios app的时候,打包需要苹果的私钥证书和证书profile文件 ...

  10. 这本vue3编译原理开源电子书,初中级前端竟然都能看懂

    前言 众所周知vue提供了很多黑魔法,比如单文件组件(SFC).指令.宏函数.css scoped等.这些都是vue提供的开箱即用的功能,大家平时用这些黑魔法的时候有没有疑惑过一些疑问呢. 我们每天写 ...