给定一个链表,删除链表的倒数第 n 个节点并返回头结点。
例如,
给定一个链表: 1->2->3->4->5, 并且 n = 2.
当删除了倒数第二个节点后链表变成了 1->2->3->5.
详见:https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/

思路:设置两个指针first跟second。first指针先移动n步,若此时first指针为空,则表示要删除的是头节点,此时直接返回head->next即可。如果first指针不为空,则将两个指针一起移动,直到first指针指向最后一个节点,令second->next=second->next->next即可删除第你n个节点。

实现语言:Java

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
if(head==null||head.next==null){
return null;
}
ListNode first=head,second=head;
for(int i=0;i<n;++i){
first=first.next;
}
if(first==null){
return head.next;
}
while(first.next!=null){
first=first.next;
second=second.next;
}
second.next=second.next.next;
return head;
}
}

参考:https://blog.csdn.net/yao_wust/article/details/41242805

019 Remove Nth Node From End of List 删除链表的倒数第N个节点的更多相关文章

  1. lintcode:Remove Nth Node From End of Lis 删除链表中倒数第n个节点

    题目: 删除链表中倒数第n个节点 给定一个链表,删除链表中倒数第n个节点,返回链表的头节点.  样例 给出链表1->2->3->4->5->null和 n = 2. 删除 ...

  2. [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 ...

  3. 【LeetCode】Remove Nth Node From End of List(删除链表的倒数第N个节点)

    这道题是LeetCode里的第19道题. 题目要求: 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, ...

  4. Leetcode19.Remove Nth Node From End of List删除链表的倒数第N个节点

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

  5. 【LeetCode】19. Remove Nth Node From End of List 删除链表的倒数第 N 个结点

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:链表, 删除节点,双指针,题解,leetcode, 力扣 ...

  6. 19 Remove Nth Node From End of List(去掉链表中倒数第n个节点Easy)

    题目意思:去掉链表中倒数第n个节点 思路:1.两次遍历,没什么技术含量,第一次遍历计算长度,第二次遍历找到倒数第k个,代码不写了   2.一次遍历,两个指针,用指针间的距离去计算. ps:特别注意删掉 ...

  7. LeetCode Remove Nth Node From End of List 删除链表的倒数第n个结点

    /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...

  8. [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 ...

  9. [Swift]LeetCode19. 删除链表的倒数第N个节点 | Remove Nth Node From End of List

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

随机推荐

  1. Boilerplate

    HTML5 Boilerplate 是一个由 Paul Irish(Google Chrome 开发人员.jQuery 项目成员.Modernizr 作者.yayQuery 播客主持人)主导的“前端开 ...

  2. jexus入门

    参考:https://www.linuxdot.net/bbsfile-3084 一.Jexus简介:Jexus web server for linux 是一款基于.NET兼容环境,运行于Linux ...

  3. 问题:oracle nvl;结果:Oracle中的NVL函数

    Oracle中的NVL函数 (2012-11-30 13:21:43) 转载▼ 标签: nvl oracle 分类: Oracle Oracle中函数以前介绍的字符串处理,日期函数,数学函数,以及转换 ...

  4. linux日常管理-linux日志

    通过系统日志获得相关信息,出现错误,突发情况可以通过查看日志获得有用的信息.遇到故障,看日志是最常用的方法,在日常工作中一定要养成看日志的习惯. 最核心的日志在这个目录下 日志会无限生成,占用的内存会 ...

  5. shell入门-awk-3

    awk的内置变量 NR 表示行 NF 表示段 显示第十行 [root@wangshaojun ~]# awk -F ':' 'NR==10' 1.txtuucp:x:10:14:uucp:/var/s ...

  6. mount/umount文件挂载

    用法: mount [-lhV] mount -a [选项] mount [选项] [--source] <源> | [--target] <目录> mount [选项] &l ...

  7. Flask16 项目结构、flask_script插件

    1 项目结构 需求:易维护.可扩展 1.1 views 处理逻辑和路由映射 C 1.2 models 模型类 M 1.3 templates 模板文件 V 1.4 static 今天文件,如:js.c ...

  8. Angular12 学习angular2前的热身准备

    1 ECMA European Computer Manufactures Association 这个组织的目标是评估,开发和认可电信和计算机标准. 百度百科:点击前往 ECMA65:满足ECMA标 ...

  9. 7、sraToolkit安装使用

    参考:http://blog.csdn.net/Cs_mary/article/details/78378552        ###prefetch 参数解释 https://www.ncbi.nl ...

  10. 截止JDK1.8版本,java并发框架支持锁包括?

    读写锁 自旋锁 乐观锁