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

  1. 题目解释:

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

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.

题意就是删除倒数第n个节点

  1. 题意分析:使用两个指针,一个快,一个慢,快的先走n步,当快的走到尾部null的时候,慢指针所指在就是要删除在那个节点。

  2. 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){return null;}ListNode fastNode = head;ListNode slowNode = head;//先提前让快指针走n步while(n-- > 0){fastNode = fastNode.next;}//记得加这一步在判断,什么情况下会为null(1->2  n = 2),翻译过来就是要删掉头部//所以就是要特殊考虑删掉头部的情况if(fastNode == null){head = head.next;return head;}//算法的核心部分while(fastNode.next != null){fastNode = fastNode.next;slowNode = slowNode.next;}//删掉倒数第n个节点的操作,将slowNode节点直接指向slowNode的next。next节点,即可删掉slowNode.next = slowNode.next.next;return head;}
}

LeetCode算法入门- Remove Nth Node From End of List -day17相关推荐

  1. LeetCode算法入门- Remove Duplicates from Sorted Array -day21

    LeetCode算法入门- Remove Duplicates from Sorted Array -day21 题目描述 Given a sorted array nums, remove the ...

  2. LeetCode算法入门- Remove Element -day20

    LeetCode算法入门- Remove Element -day20 1. 题目描述 Given an array nums and a value val, remove all instance ...

  3. 【LeetCode】19. 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, Give ...

  4. 【Leetcode】19. Remove Nth Node From End of List (cpp)

    讲思路的文章已经很多了,不再赘述,这里用的方法是两个指针一次遍历算法.想强调的是有可能要删除的就是第一个结点.删除头节点比较方便的做法是加一个伪头节点. /*** Definition for sin ...

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

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

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

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

  7. Remove Nth Node From End of List - LeetCode

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

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

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

  9. LeetCode算法入门- Longest Substring Without Repeating Characters-day4

    LeetCode算法入门- Longest Substring Without Repeating Characters-day4 Longest Substring Without Repeatin ...

最新文章

  1. 【uniapp】3d轮播图/堆叠轮播图/层叠轮播图
  2. Spring JSR-250 注释
  3. UVA 116——Unidirectional TSP
  4. shell 中 $(( )) 与 $( ) 还有 ${ } 的区别
  5. iptables控制较复杂案例
  6. JavaScript 字符串属性和方法
  7. power bi可视化表_滚动器可视化功能,用于Power BI Desktop中的股价变动
  8. IOS-40-测试环境与正式环境的代码通过标示符来区分
  9. 可见光和红外图像的融合
  10. 微软模拟飞行2020 数字化建模分析
  11. C# Spire操作Word文档生成PDF或JPG格式
  12. 小甲鱼C++ 1 语言与思想介绍
  13. 仿百度文库文档上传页面的多级联动分类选择器
  14. linux安装java.jdk环境
  15. “辶”“廴”偏旁的字五笔拆字
  16. MEMS智能传感器技术的新进展
  17. 高斯金字塔和拉普拉斯金字塔实现图像融合
  18. 2019年,微信多开软件排行榜
  19. EXCEL中 xls 与xlsx 的区别
  20. execl(知道每个商品单价,且知道总价,计算每个商品的数量)

热门文章

  1. c ++查找字符串_C ++结构| 查找输出程序| 套装3
  2. Java LocalDateTime类| 带示例的getDayOfYear()方法
  3. python中的Lambda表达式/函数
  4. jQuery.html()方法ie下不能设置html代码的问题
  5. 第二章 认识计算机硬件
  6. zoj 1091 Knight Moves
  7. PyQt5在对话框中打开外部链接的方法
  8. Python破解滑块验证码算法,完美避开人机识别
  9. jsessionid每次请求都在变_为什么每次沟通都变争吵,学会这4条沟通原则,解决90%的沟通问题...
  10. python宏替换_简单的宏替换