题目:

Given a linked list, remove the nth node from the end of list and return its head.
For example,

Given linked list: 1->2->3->4->5, and n = 2.

After removing the second node from the end, the linked list becomes 1->2->3->5.

Note:
Given n will always be valid.
Try to do this in one pass.

思路:

三个指针p1,p2,p3,让第一指针p1走n - 1步停下来,然后p1和p2一起往前走,同时保持一个指针指向p2的前一个节点。

package list;public class RemoveNthNodeFromEndOfList {public ListNode removeNthFromEnd(ListNode head, int n) {ListNode p1 = head;ListNode p2 = head;ListNode prevp2 = new ListNode(0);prevp2.next = p2;head = prevp2;while (--n > 0) {p1 = p1.next;}while (p1.next != null) {p1 = p1.next;prevp2 = p2;p2 = p2.next;}prevp2.next = p2.next;return head.next;}public static void main(String[] args) {// TODO Auto-generated method stub
}}

转载于:https://www.cnblogs.com/null00/p/5060233.html

LeetCode - Remove Nth Node From End of List相关推荐

  1. LeetCode:Remove Nth Node From End of List

    题目链接 Given a linked list, remove the nth node from the end of list and return its head. For example, ...

  2. [LeetCode]Remove Nth Node From End of List

    又一道链表题.CareerCup上也有类似题目,trick和"部分链表逆序"一样,由于不知道链表的长度,second指针比first指针先走n步,然后在同时向前移动,直到secon ...

  3. LeetCode Remove Nth Node From End of List

    题意:给出一个单链表,删除从尾部开始的第n个元素 思路: 从头开始遍历n个元素,此时当前结点指向头开始的第n+1个元素.继续遍历同时,另外一个结点记录从头开始至遍历结束时的所经历的结点,这个结点就是要 ...

  4. Remove Nth Node From End of List - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Remove Nth Node From End of List - LeetCode 注意点 考虑删除的结点是开头的那个结点 输入给的链表是没有开头的& ...

  5. LeetCode算法入门- Remove Nth Node From End of List -day17

    LeetCode算法入门- Remove Nth Node From End of List -day17 题目解释: Given a linked list, remove the n-th nod ...

  6. LeetCode:Remove Nth Node From End of List 移除链表倒第n项

    2019独角兽企业重金招聘Python工程师标准>>> 1.题目名称 Remove Nth Node From End of List(移除链表中倒数第n项) 2.题目地址 http ...

  7. LeetCode 19. Remove Nth Node From End of List

    LeetCode 19. Remove Nth Node From End of List Solution1:我的答案 并不算是最优解法. /*** Definition for singly-li ...

  8. 数据结构与算法 | Leetcode 19. Remove Nth Node From End of List

    原文链接:https://wangwei.one/posts/jav... 前面,我们实现了 两个有序链表的合并 操作,本篇来聊聊,如何删除一个链表的倒数第N个节点. 删除单链表倒数第N个节点 Lee ...

  9. LeetCode19. Remove Nth Node From End of List 删除链表中的倒数第n个位置的元素

    前言 本文是LeetCode19. Remove Nth Node From End of List解法,这个题目需要删除链表中的倒数第n个位置的元素 代码 # -*- coding: utf-8 - ...

最新文章

  1. TS - 处理故障的一些通用方法
  2. Linux检查镜像,Shell脚本实现检测Cygwin最快的镜像站点
  3. Embedding技术在房产推荐中的应用
  4. OFFICE——Word与Excel交互处理——邮件合并
  5. web前端表格css三个t的使用(thead,tbody,tfoot)
  6. python 量化交易_基于Python的量化交易工具清单(上)
  7. mysql select in 不存在返回0_MySQL索引优化看这篇文章就够了!
  8. Pandas DataFrame 去重
  9. c++ 输出二进制_Q音直播编译优化与二进制集成方案
  10. Excel2010学习笔记(二):公式函数篇
  11. spring boot ---- jpa连接和操作mysql数据库
  12. uva 10817 - Headmaster's Headache ( 状态压缩dp)
  13. scratch---植物大战僵尸游戏实现,完整代码分享,多种僵尸与植物特效都有素材包!
  14. 2019年计算机设计大赛(省赛)
  15. PayPal提现银行不给入账要退回?最新解决方法如下!!!
  16. 关于《微习惯》的记录
  17. cocos2d实现画笔功能及常用的绘制函数
  18. 正在移除icloud数据久_深度梳理一下iOS照片(相册)的逻辑,和减少iCloud的空间占用的方法...
  19. 武汉星起航跨境电商——亚马逊日本站JCT政策将实现改革
  20. DIY智能家居语音助理——语音智控万物

热门文章

  1. python3安装教程linux_python 在linux系统的安装教程
  2. 【算法】剑指 Offer 57. 和为s的两个数字
  3. 【Elasticsearch】ElasticSearch 7.8 多字段权重排序
  4. 【Elasticsearch】 es 集群发现机制
  5. 【netty】Flink Clickhouse 写入失败 Channel output shutdown Broken pipe
  6. 【Flink】数据传输 挖个坑 把自己埋了 ClassCastException String cannot be cast to [LJava.lang.String
  7. 【MySQL】MySQL drop,truncate,delete 区别
  8. Spark: Structured + hive(Jdbc方式)卡死
  9. 【Flink】Flink的窗口触发器 PurgingTrigger
  10. Mac使用终端压缩加密文件