【每日一题】Leetcode - 19. Remove Nth Node From End of List

这篇具有很好参考价值的文章主要介绍了【每日一题】Leetcode - 19. Remove Nth Node From End of List。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Question

Leetcode - 19. Remove Nth Node From End of List

Train of thought

Drawing on the method of finding midpoints in linked lists, use quick slow pointer

Finding midpoints in linked lists

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {

    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode pioneer = head, offspring = head, bHead = head;
        while (n-- > 0 && head != null) {
            pioneer = head.next;
            head = head.next;
        }

        if (pioneer == null) {
            return bHead.next;
        }
 
        while (pioneer.next != null) {
            offspring = offspring.next;
            pioneer = pioneer.next;
        }

        offspring.next = offspring.next.next;

        return bHead;
    }

}

【每日一题】Leetcode - 19. Remove Nth Node From End of List,每日一题,leetcode,算法,java,快慢指针

Optimize

nothing文章来源地址https://www.toymoban.com/news/detail-528331.html

到了这里,关于【每日一题】Leetcode - 19. Remove Nth Node From End of List的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

  • Leetcode 82. Remove Duplicates from Sorted List II

    Remove Duplicates from Sorted List II Medium Given the head of a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Return the linked list sorted as well. Example 1: Input: head = [1,2,3,3,4,4,5] Output: [1,2,5] Example 2: Input: head = [1,1,1,2,3] Output: [2,3] Constraints: The number of n

    2024年02月10日
    浏览(7)
  • LeetCode //C - 82. Remove Duplicates from Sorted List II

    LeetCode //C - 82. Remove Duplicates from Sorted List II

    Given the head of a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list . Return the linked list sorted as well.   Example 1: Input: head = [1,2,3,3,4,4,5] Output: [1,2,5] Example 2: Input: head = [1,1,1,2,3] Output: [2,3] Constraints: The number of nodes in the list is in the range [0, 300]

    2024年02月10日
    浏览(6)
  • 每日一题2023.7.19|ACM模式

    每日一题2023.7.19|ACM模式

    参考博客 最基本,最常用的字符或者数字的输入方式。在输入过程中会过滤掉不可见字符、如空格、回车、tab。若不想过滤掉空白字符,可以使用noskipws流进行控制。 运行结果 遇到空格回车等会结束获取输入的字符串,后面的字符串会被过滤掉(存放在输入流中),如果后面还

    2024年02月16日
    浏览(36)
  • 【论文笔记】An End-to-End Framework of Road User Detection, Tracking, and Prediction from Monocular Images

    【论文笔记】An End-to-End Framework of Road User Detection, Tracking, and Prediction from Monocular Images

    原文链接:https://arxiv.org/abs/2308.05026 目前的轨迹预测方法多基于道路使用者的真实信息,但在实际自动驾驶系统中这些信息是通过检测和跟踪模块得到的,不可避免的存在噪声。本文将感知模块与轨迹预测整合,进行端到端的检测、跟踪和轨迹预测。 本文感知模块使用单目图

    2024年04月28日
    浏览(11)
  • [Week 19]每日一题(C++,数学,并查集,动态规划)

    [Week 19]每日一题(C++,数学,并查集,动态规划)

    目录 [Daimayuan] T1 倒数第n个字符串(C++,进制) 输入格式 输出格式 样例输入 样例输出 解题思路 [Daimayuan] T2 排队(C++,并查集) 输入格式 输出格式 样例输入1 样例输出1 样例输入2 样例输出2 样例输入3 样例输出3 数据规模 解题思路 [Daimayuan] T3 素数之欢(C++,BFS) 数据规模

    2024年02月02日
    浏览(10)
  • 客户端Unexpected end of file from server 和 服务端nginx 408 的一种解决方法

    客户端Unexpected end of file from server 和 服务端nginx 408 的一种解决方法

    https://www.jiweichengzhu.com/article/81044c11caf54753aeed94ef8fad1070 https://blog.csdn.net/chenz_yang/article/details/77238532 终端设备(Debian系统、戴尔服务器)通过HTTPS向云端上报数据时,有的数据如设备心跳能上报,有的数据如服务器配置信息上报时报错: Unexpected end of file from server “Unexpected en

    2024年02月15日
    浏览(9)
  • 【每日一题Day245】面试题 16.19. 水域大小 | dfs

    你有一个用于表示一片土地的整数矩阵 land ,该矩阵中每个点的值代表对应地点的海拔高度。若值为0则表示水域。由垂直、水平或对角连接的水域为池塘。池塘的大小是指相连接的水域的个数。编写一个方法来计算矩阵中所有池塘的大小,返回值需要从小到大排序。 dfs感染

    2024年02月10日
    浏览(10)
  • leetcode每日一题44

    图论 dfs/bfs dfs代码框架 思路:本题要求找到被x围绕的陆地,所以边界的陆地O肯定不符合条件。那么我们只要从周边找到陆地O然后 通过 dfs或者bfs 将周边靠陆地且相邻的陆地O都变成A,然后再去重新遍历地图的时候,把剩下的O变成X,再把所有的A变成O。 确认递归函数,参数

    2024年01月19日
    浏览(15)
  • LeetCode每日一题之 复写0

    LeetCode每日一题之 复写0

    目录 题目介绍: 算法原理: 特殊位置处理: 代码实现: 题目链接:. - 力扣(LeetCode) 这种对数组元素进行修改,移动的题目我们仍然可以使用双指针法,不过我们按照常规思路从左到右处理数组,不难发现如下这种问题: 当cur指向1时,让dest下一个元素复写cur指向的元素

    2024年04月23日
    浏览(15)
  • 【LeetCode】每日一题:移除元素

    【LeetCode】每日一题:移除元素

    目录  题目: 思想1:暴力解法 思想2:创建一个temp数组  思想3:双指针 👻内容专栏:《LeetCode刷题专栏》 🐨本文概括: 27.移除元素 🐼本文作者:花 碟 🐸发布时间:2023.4.15 https://leetcode.cn/problems/remove-element/   点击跳转到LeetCode平台OJ页面(27.移除元素)  👉 示例1: 解

    2023年04月19日
    浏览(14)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包