19. 删除链表的倒数第 N 个结点

java 复制代码
/**
 * 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) {
        //注意初始化
        int s_fast = 1;
        ListNode dummyhead = new ListNode();
        dummyhead.next = head;
        ListNode fast = dummyhead;
        ListNode slow = dummyhead;
        while(fast!=null){
            fast = fast.next;
            if (s_fast++<=n+1){
                continue;
            }else{
                slow = slow.next;
            }    
        }
        ListNode temp = slow.next.next;
        slow.next = temp;
        return dummyhead.next;

    }
}
相关推荐
Forever Nore1 小时前
LeetCode 13 罗马数字转整数 - 按规则处理
linux·服务器·leetcode
旖旎夜光2 小时前
LeetCode 904:水果成篮(滑动窗口) —— 题解
数据结构·c++·算法·leetcode·滑动窗口
ZC跨境爬虫3 小时前
LeetCode 27. 移除元素(双指针详解 + Java Python 多解法对比)
java·python·leetcode
LuminousCPP6 小时前
单链表专题(四)-刷题复盘篇-LeetCode 138 随机链表复制|原地拷贝法突破复杂指针操作
数据结构·笔记·算法·leetcode·链表
Forever Nore6 小时前
LeetCode 14 最长公共前缀 - 纵向扫描
linux·服务器·leetcode
圣保罗的大教堂6 小时前
leetcode 3090. 每个字符最多出现两次的最长子字符串 简单
leetcode
旖旎夜光20 小时前
LeetCode 3:无重复字符的最长子串(滑动窗口) —— 题解
数据结构·c++·算法·leetcode·滑动窗口
Navigator_Z1 天前
LeetCode //C - 1192. Critical Connections in a Network
c语言·算法·leetcode
rannn_1111 天前
【力扣hot100】链表专题下|138、148、23、146
java·算法·leetcode·链表·开发
hanlin031 天前
刷题笔记:力扣第189题-轮转数组
笔记·算法·leetcode