删除链表倒数第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 ...
随机推荐
- java --面试题大全
J2EE面试题 文档版本号:V2.0 2016年11月 目 录 1. Java基础部分 8 1.1. 一个".java"源文 ...
- 记一次aspnetcore发布部署流程初次使用k8s
主题: aspnetcorewebapi项目,提交到gitlab,通过jenkins(gitlab的ci/cd)编译.发布.推送到k8s. 关于gitlab.jenkins.k8s安装,都是使用doc ...
- Python2 SyntaxError: Non-ASCII character '\xc3' in file...
使用pip安装完需要的python库,运行脚本时报错:Syntax Error: Non-ASCII character '\xc3' in file /usr/local/lib/python2.7 ...
- VulnHub-Narak靶机渗透流程
VulnHub-Narak Description Narak is the Hindu equivalent of Hell. You are in the pit with the Lord of ...
- yb课堂实战之订单和播放记录事务控制 《十六》
开启事务控制 启动类:@EnableTransactionManagement 业务类,或者业务方法@Transactional 默认事务的隔离级别和传播属性 启动类上加注解 Service层加注解
- ES6拼接数组与小程序本地存储
拼接数组 ES6扩展运算符[三个点(...)将一个数组转为用逗号分隔的参数序列] goodsList: [...goodsList, ...goods] 本地存储 // 把接口数据存入本地存储中 wx ...
- 在Visual Studio Code中,鼠标双击PHP变量的时候,如何选择包括$在内的整个变量名
依次点击:文件->首选项->设置 并在"editor.wordSeparators"设置中为您的语言指定删除"$"符号:
- mysql:Windows修改MySQL数据库密码(修改或忘记密码)
今天练习远程访问数据库时,为了方便访问,就想着把数据库密码改为统一的,以后我们也会经常遇到MySQL需要修改密码的情况,比如密码太简单.忘记密码等等.在这里我就借鉴其他人的方法总结几种修改MySQL密 ...
- WRONG(COPY)
去年总结的列表,欢迎大家补充!! 两个int相乘,50%几率会爆了int.(不开long long见祖宗) 无向图邻接表的边表忘了这是心口永远的痛: 线段树数组开小是4(乘4有时候不够) 调用多个函数 ...
- idea使用git管理项目(Mac版)
1.本地安装git mac版 breaw install git 查看是否安装成功 git --version 这样就成功了,一般是自带的有 windows版 https://www.cnblogs. ...