删除链表倒数第N个节点(19)
双指针法
双指针法主要是最开始有两个指针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)的更多相关文章
- [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 ...
- Leetcode算法系列(链表)之删除链表倒数第N个节点
Leetcode算法系列(链表)之删除链表倒数第N个节点 难度:中等给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点.示例:给定一个链表: 1->2->3->4-&g ...
- 19。删除链表倒数第N个节点
class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next# 这道题还是很简单的,我们只 ...
- 删除链表倒数第n个节点
题目: 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第二个节点后,链 ...
- LeetCode 删除链表倒数第N个节点
基本思路 定义两个指示指针a b 让a先行移动n+1个位置 若a指向了NULL的位置,则删除的是头节点(由于走过了n+1个节点刚好指在尾部的NULL上) 否则让b与a一起移动直至a->next, ...
- [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 ...
- 删除单链表倒数第n个节点
基本问题 如何删除单链表中的倒数第n个节点? 常规解法 先遍历一遍单链表,计算出单链表的长度,然后,从单链表头部删除指定的节点. 代码实现 /** * * Description: 删除单链表倒数第n ...
- leetcode 19.删除链表的第n个节点
删除链表的第n个节点 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第 ...
- [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 ...
- [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 ...
随机推荐
- 全志科技T3国产工业核心板规格书(四核ARM Cortex-A7,主频1.2GHz)
1 核心板简介 创龙科技SOM-TLT3是一款基于全志科技T3处理器设计的4核ARM Cortex-A7国产工业核心板,每核主频高达1.2GHz. 核心板通过邮票孔连接方式引出CSI.TVIN.MIP ...
- vulnhub - ASSERTION1.0.1
vulnhub - ASSERTION 1.0.1 信息收集 nmap -sT --min-rate 10000 -p- 192.168.157.171 sudo nmap -sT -sV -sC - ...
- HBase 在统一内容平台业务的优化实践
作者:来自 vivo 互联网服务器团队-Leng Jianyu.Huang Haitao HBase是一款开源高可靠性.扩展性.高性能和灵活性的分布式非关系型数据库,本文围绕数据库选型以及使用HBas ...
- 1. C++ 开发环境
C++ 开发环境 Visual C++ / GCC(G++) / Clang(Clang++) 集成开发环境:Visual Studio / CodeLite / Code::blocks / CLi ...
- webpack4.15.1 学习笔记(四) — Tree shaking
目录 Tree shaking 原理 标记效果 副作用代码不可被删除 如何实现 Tree shaking 的几种方法 总结 Tree shaking 本质上为了消除无用的js代码,减少加载文件体积的方 ...
- VS Code 开发统一代码格式化配置
eslint: 是用来做代码风格检查的,比较关注代码质量,并且会提示不符合风格规范的代码,也有一部分代码格式化的功能.不是消除空行. "editor.formatOnSave": ...
- Netcode for Entities如何添加自定义序列化,让GhostField支持任意类型?以int3为例(1.2.3版本)
一句话省流:很麻烦也很抽象,能用内置支持的类型就尽量用. 首先看文档.官方文档里一开头就列出了所有内置的支持的类型:Ghost Type Templates 其中Entity类型需要特别注意一下:在同 ...
- Django--StreamingHttpResponse下载文件
from django.shortcuts import render, HttpResponse from django.http import StreamingHttpResponse impo ...
- RPC接口测试(六)RPC协议解析(重要!重要!重要!)
RPC协议解析 RPC(Remote Procedure Call Protocol)远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议.简言之,RPC使得程 ...
- 【Git】01 下载安装(Windows)
Git 官网地址:[点我访问] https://git-scm.com/ 点击这个电脑自动识别操作系统与系统位数 开始安装 安装的目录不要有中文就行[最好也不要有空格] 算了,直接全选[小孩子才做选择 ...