【每日一题】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 //80. Remove Duplicates from Sorted Array II

    Given an integer array nums sorted in non-decreasing order, remove some duplicates in-place such that each unique element appears at most twice. The relative order of the elements should be kept the same. Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums

    2024年02月13日
    浏览(37)
  • 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日
    浏览(33)
  • 每日一题2023.7.19|ACM模式

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

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

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

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

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

    2024年02月02日
    浏览(39)
  • 客户端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日
    浏览(50)
  • 【每日一题Day245】面试题 16.19. 水域大小 | dfs

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

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

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

    2024年01月19日
    浏览(35)
  • 【LeetCode】每日一题:移除元素

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

    2023年04月19日
    浏览(41)
  • Leetcode每日一题——“移除元素”

    各位CSDN的uu们你们好呀,小雅兰又来啦,今天,小雅兰的内容是移除元素,下面,让我们进入Leetcode的世界吧   说明: 为什么返回数值是整数,但输出的答案是数组呢? 请注意,输入数组是以「引用」方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。 你可以

    2023年04月23日
    浏览(45)

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

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

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

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

二维码1

领取红包

二维码2

领红包