leetcode 热题 100_删除链表的倒数第 N 个结点

题解一:

        递归:利用递归栈逆向遍历链表,并用全局变量记录当前遍历的是倒数第几位节点,当遍历到待删节点的上一位节点时,node.next=node.next.next删除待删节点。需要注意当删除的是头节点时,直接return head.next删除头节点。

class Solution {
    int temp;//全局变量记录遍历位置

    public int F(ListNode head) {
        if (head != null) {
            if (F(head.next) == 0) {
                head.next = head.next.next;
            }
        }
        return temp--;
    }


    public ListNode removeNthFromEnd(ListNode head, int n) {
        temp = n;
        F(head);
        if (temp == -1) return head.next;
        return head;
    }
}

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-14 01:10:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-14 01:10:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-14 01:10:02       82 阅读
  4. Python语言-面向对象

    2024-03-14 01:10:02       91 阅读

热门阅读

  1. 24计算机考研调剂 | 齐齐哈尔大学

    2024-03-14 01:10:02       51 阅读
  2. Ubuntu用扩展分区加载home目录步骤

    2024-03-14 01:10:02       42 阅读
  3. 计算机网络 基础知识 套接字 编程

    2024-03-14 01:10:02       41 阅读
  4. 哈希冲突

    2024-03-14 01:10:02       43 阅读
  5. 函数式编程之参数

    2024-03-14 01:10:02       39 阅读
  6. 453453

    2024-03-14 01:10:02       41 阅读
  7. android pdf框架-7,白边切割

    2024-03-14 01:10:02       41 阅读
  8. 贪心算法|【简介】

    2024-03-14 01:10:02       37 阅读
  9. SSL证书的定义

    2024-03-14 01:10:02       45 阅读
  10. LJXpaper

    LJXpaper

    2024-03-14 01:10:02      40 阅读
  11. 传统开发读写优化与HBase

    2024-03-14 01:10:02       43 阅读
  12. MYSQL集群

    2024-03-14 01:10:02       35 阅读
  13. 翻硬币..

    2024-03-14 01:10:02       48 阅读
  14. vue element plus Select 选择器

    2024-03-14 01:10:02       37 阅读