基本问题

如何删除单链表中的倒数第n个节点?

常规解法

先遍历一遍单链表,计算出单链表的长度,然后,从单链表头部删除指定的节点。

代码实现

  1. /**
  2. *
  3. * Description: 删除单链表倒数第n个节点,常规解法.
  4. *
  5. * @param head
  6. * @param n
  7. * @return ListNode
  8. */
  9. public static ListNode removeNthFromEnd(ListNode head, int n) {
  10. if (head == null)
  11. return null;
  12. //get length of list
  13. ListNode p = head;
  14. int len = 0;
  15. while (p != null) {
  16. len++;
  17. p = p.next;
  18. }
  19. //if remove first node
  20. int fromStart = len - n + 1;
  21. if (fromStart == 1)
  22. return head.next;
  23. //remove non-first node
  24. p = head;
  25. int i = 0;
  26. while (p != null) {
  27. i++;
  28. if (i == fromStart - 1) {
  29. p.next = p.next.next;
  30. }
  31. p = p.next;
  32. }
  33. return head;
  34. }

一次遍历法

使用快慢指针。快指针比慢指针提前n个单元。当快指针到达单链表尾部时,慢指针指向待删除节点的前节点。

代码实现

  1. /**
  2. *
  3. * Description: 删除单链表倒数第n个节点,快慢指针法.
  4. *
  5. * @param head
  6. * @param n
  7. * @return ListNode
  8. */
  9. public static ListNode removeNthFromEnd(ListNode head, int n) {
  10. if (head == null)
  11. return null;
  12. ListNode fast = head;
  13. ListNode slow = head;
  14. for (int i = 0; i < n; i++) {
  15. fast = fast.next;
  16. }
  17. //if remove the first node
  18. if (fast == null) {
  19. head = head.next;
  20. return head;
  21. }
  22. while (fast.next != null) {
  23. fast = fast.next;
  24. slow = slow.next;
  25. }
  26. slow.next = slow.next.next;
  27. return head;
  28. }

删除单链表倒数第n个节点的更多相关文章

  1. 【链表问题】打卡2:删除单链表的第 K个节点

    前言 以专题的形式更新刷题贴,欢迎跟我一起学习刷题.每道题会提供简单的解答. 题目描述 在单链表中删除倒数第 K 个节点 要求 如果链表的长度为 N, 时间复杂度达到 O(N), 额外空间复杂度达到 ...

  2. leetcode 去除单链表倒数第k个节点

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

  3. 单链表倒数第K个节点的查找和显示

    1.使用一个固定长度队列装链表段,当遍历到链表根时,返回队列头元素. class Node{ int value; Node next; public Node(int value){ this.va ...

  4. 19. Remove Nth Node From End of List【Medium】【删除单链表倒数第n个结点】

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

  5. lintcode :nth to Last Node In List 链表倒数第n个节点

    题目: 链表倒数第n个节点 找到单链表倒数第n个节点,保证链表中节点的最少数量为n. 样例 给出链表 3->2->1->5->null和n = 2,返回倒数第二个节点的值1. ...

  6. 链表倒数第n个节点

    找到单链表倒数第n个节点,保证链表中节点的最少数量为n. 样例 给出链表 3->2->1->5->null和n = 2,返回倒数第二个节点的值1. /** * Definiti ...

  7. lintcode166 链表倒数第n个节点

    链表倒数第n个节点 找到单链表倒数第n个节点,保证链表中节点的最少数量为n. 思路:设置两个指针first,second指向head,first指针先向前走n,然后两个指针一起走,first指针走到末 ...

  8. LintCode 链表倒数第n个节点

    找到单链表倒数第n个节点,保证链表中节点的最少数量为n. 样例 给出链表 3->2->1->5->null和n = 2,返回倒数第二个节点的值1. 分析:设两个指针 p1和p2 ...

  9. Leetcode算法系列(链表)之删除链表倒数第N个节点

    Leetcode算法系列(链表)之删除链表倒数第N个节点 难度:中等给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点.示例:给定一个链表: 1->2->3->4-&g ...

随机推荐

  1. Word2010撤销按钮失效,Ctrl+Z失效解决办法

    1.打开注册表编辑器.按Win+R,在运行框中键入regedit,然后单击“确定”. 2.在注册表编辑器中,展开到下列注册表子项: HKEY_CURRENT_USER\Software\Microso ...

  2. Android Launcher 研究学习

    Launcher是系统启动后第一个启动的程序,是其它应用程序的入口,也就是我们的手机程序的桌面程序; 一.Launcher的定义及构成: <1>通过查看官方提供的Launcher源码可以知 ...

  3. python 字符串截取

    我们可以通过索引来提取想要获取的字符,可以把python的字符串也做为字符串的列表就更好理解 python的字串列表有2种取值顺序1是从左到右索引默认0开始的,最大范围是字符串长度少1s = 'ilo ...

  4. node.js 的 os 模块

    Node.js的os module 提供了一系列跟操作系统相关的操作函数,比较简单,所以功能也就十分有限.我们可以去官网看各个函数的介绍: http://nodejs.org/api/os.html ...

  5. [JAVA词形还原工具]Snowball

    demo:http://snowball.tartarus.org/demo.php jar download:http://snowball.tartarus.org/download.php (J ...

  6. sql: 查找约束

    主键约束 SELECT   tab.name AS [表名],   idx.name AS [主键名称],   col.name AS [主键列名] FROM   sys.indexes idx    ...

  7. 解决访问StackOverFlow太慢的问题

    Stackoverflow加载时访问了被屏蔽的站点ajax.googleapis.com,导致加载缓慢,把这个站点加到Hosts里,指向127.0.0.1即可

  8. [IR] Index Construction

    Three steps to construct Inverted Index as following: 最难的step中: Token sequence. Sort by term. Dictio ...

  9. [Node.js] Node + Redis 实现分布式Session方案

    原文地址: http://www.moye.me/?p=565 Session是什么? Session 是面向连接的状态信息,是对 Http 无状态协议的补充. Session 怎么工作? Sessi ...

  10. Python开源框架Scrapy安装及使用

    一.安装问题 环境: CentOS  + Python 2.7 + Pip 1) 安装时遇到 ”UnicodeDecodeError: 'ascii' codec can't decode byte  ...