[leetcode]删除链表中倒数第k个结点

. - 力扣(LeetCode)

class Solution {
public:
    ListNode* trainningPlan(ListNode* head, int cnt) {
        int n = 0;
        ListNode* node = nullptr;

        for (node = head; node; node = node->next) {
            n++;
        }
        for (node = head; n > cnt; n--) {
            node = node->next;
        }
      
        return node;
    }
};

class Solution {
public:
    ListNode* trainningPlan(ListNode* head, int cnt) {
        ListNode* fast = head;
        ListNode* slow = head;

        while (fast && cnt > 0) {
            fast = fast->next;
            cnt--;
        }
        while (fast) {
            fast = fast->next;
            slow = slow->next;
        }

        return slow;
    }
};

相关推荐

  1. leetcode19. 删除倒数 N

    2024-06-12 11:46:05       51 阅读
  2. Leetcode】19. 删除倒数 N

    2024-06-12 11:46:05       62 阅读

最近更新

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

    2024-06-12 11:46:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-12 11:46:05       100 阅读
  3. 在Django里面运行非项目文件

    2024-06-12 11:46:05       82 阅读
  4. Python语言-面向对象

    2024-06-12 11:46:05       91 阅读

热门阅读

  1. tensorflow安装

    2024-06-12 11:46:05       32 阅读
  2. Web前端图片并排显示的艺术与技巧

    2024-06-12 11:46:05       27 阅读
  3. ubuntu安装deb解决依赖关系错误问题

    2024-06-12 11:46:05       25 阅读
  4. Webapp前端框架模板:探索、实践与创新

    2024-06-12 11:46:05       31 阅读
  5. 服务器硬件基础知识

    2024-06-12 11:46:05       25 阅读
  6. Linux基础命令

    2024-06-12 11:46:05       23 阅读
  7. 大数据知识分享:Python中的变量

    2024-06-12 11:46:05       28 阅读
  8. 力扣981.基于时间的键值存储

    2024-06-12 11:46:05       27 阅读
  9. 【rust 第三方库】serde 序列化反序列化框架

    2024-06-12 11:46:05       27 阅读
  10. 分布式系统

    2024-06-12 11:46:05       24 阅读
  11. Python语言在金融领域的应用探索

    2024-06-12 11:46:05       25 阅读