首页 > 其他分享 >LeetCode- 19 删除链表的倒数第N个节点

LeetCode- 19 删除链表的倒数第N个节点

时间:2024-04-17 23:23:03浏览次数:24  
标签:current head ListNode 19 next 链表 int return LeetCode

题目地址

https://leetcode.cn/problems/remove-nth-node-from-end-of-list/description/

参考实现

 /**
     * 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; }
     * }
     */
    /**
     * 通过链表长度 进行处理
     *
     * @param head
     * @param n
     * @return
     */
    public static ListNode removeNthFromEnd1(ListNode head, int n) {
        ListNode dumny = new ListNode(0, head);
        ListNode current = dumny;
        for (int i = 1; i < getLenth1(head) - n + 1; i++) {
            current = current.next;
        }
        //执行删除
        current.next = current.next.next;
        return dumny.next;

    }

    /**
     * 获取链表大小
     *
     * @param head
     * @return
     */
    public static int getLenth1(ListNode head) {
        int length = 0;
        while (head != null) {
            head = head.next;
            length++;
        }

        return length;
    }

    /**
     * 利用栈特性处理
     *
     * @param head
     * @param n
     * @return
     */
    public static ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dumny = new ListNode(0, head);
        Deque<ListNode> stack = (Deque<ListNode>) new LinkedList<ListNode>();
        ListNode current = dumny;
        while (current != null) {
            stack.push(current);
            current = current.next;
        }

        for (int i = 0; i < n; i++) {
            stack.pop();
        }

        ListNode peek = stack.peek();
        peek.next = peek.next.next;
        return dumny.next;
    }

    /**
     * 双指针处理
     *
     * @param head
     * @param n
     * @return
     */
    public static ListNode removeNthFromEnd3(ListNode head, int n) {
        ListNode dummy = new ListNode(0, head);
        ListNode first = head;
        ListNode second = dummy;
        for (int i = 0; i < n; i++) {
            first = first.next;
        }
        while (first != null) {
            first = first.next;
            second = second.next;
        }
        second.next = second.next.next;
        return dummy.next;
    }

 

标签:current,head,ListNode,19,next,链表,int,return,LeetCode
From: https://www.cnblogs.com/wdh01/p/18142041

相关文章

  • LeetCode三则
    198.打家劫舍你是一个专业的小偷,计划偷窃沿街的房屋。每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警。给定一个代表每个房屋存放金额的非负整数数组,计算你在不触动警报装置的情况......
  • LeetCode两则
    1137.第N个泰波那契数泰波那契序列Tn定义如下:T0=0,T1=1,T2=1,且在n>=0的条件下Tn+3=Tn+Tn+1+Tn+2给你整数n,请返回第n个泰波那契数Tn的值。示例1:输入:n=4输出:4解释:T_3=0+1+1=2T_4=1+1+2=4示例2:输入:n=25输出:1389537提示:0<=n......
  • CF1939C
    题意有一个会重复k次的数组,每个人都可以那不超过t种礼物,礼物必须是顺序发的,不能第一个发给第二个,求最少的人的个数。首先每一个人都必须要取尽可能多的礼物,然后按这个模拟即可。那么我们就要搞出每一个点往后跳到那里。这个最容易想到的应该是主席树加二分,根据两只\(\log\)跑得......
  • P10342 [THUSC 2019] 数列 题解
    形式化题面:求\[\sum_{l=1}^{n}\sum_{r=l}^{n}\max_{i=l}^{r}(i-l+1)\timesf(i,r)\]其中\(f(l,r)\)为\(a_l,...,a_r\)中有多少个不同的数字。注意到,除了Sub2,其余数据点都有\(\maxf\le800\),这启发我们考虑\(O(nm)\)的算法。套路地,扫描线枚举右端点,则现在只需要考虑......
  • CF1955H The Most Reckless Defense
    给敌人加血可以看成是减少防御塔的攻击力,那么一个塔对敌人能造成的最大伤害即为\(500\pir^2-3^r\),注意到\(r=12\)时已经小于\(0\)了,所以半径不会很大,又因为每个\(r\)只能选一次,所以有效的塔很少,考虑状压\(dp\)。具体地,我们设\(f_{i,S}\)表示前\(i\)个塔中,被选到的塔......
  • LeetCode 面试经典150题---007
    ####13.罗马数字转整数罗马数字包含以下七种字符:I,V,X,L,C,D和M。字符数值I1V5X10L50C100D500M1000例如,罗马数字2写做II,即为两个并列的1。12写做X......
  • LeetCode 1315. Sum of Nodes with Even-Valued Grandparent
    原题链接在这里:https://leetcode.com/problems/sum-of-nodes-with-even-valued-grandparent/description/题目:Giventhe root ofabinarytree,return thesumofvaluesofnodeswithan even-valuedgrandparent.Iftherearenonodeswithan even-valuedgrandpar......
  • cmake调用VS2019的v140(VS2015)的工具链编译通过
    最近的工作基本上在Linux上做。但是,也有一个小工具需要同时支持Windows/Linux,工具依赖于Qt,从官方下载的版本上有qt5.6.3/5.12.12,这两个版本都有MSVC2015。因此搞了一个Win7的虚拟机,VS2015实在太大了,从VS2019的社区版看到可以定制仅安装C++工具链即可。VS2019裁剪最小项C++核......
  • AtCoder Beginner Contest 319
    A-LegendaryPlayers#include<bits/stdc++.h>usingnamespacestd;intmain(){map<string,string>h;h["tourist"]="3858";h["ksun48"]="3679";h["Benq"]="3658"......
  • [BSidesCF 2019]Kookie
    [BSidesCF2019]Kookie提示我们使用admin账户登录,并且明示了当前存在一个cookie账户其密码为monster登录并抓包,可以观察到设置了一个Cookie,内容为username=cookie的键值对。显然这里Cookie中的键值对的值作为了服务端在用户通过账户密码登录之后再次访问时验证身份的凭证,将其......