力扣19 删除链表的第N个结点

cpp 复制代码
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        int length=0;
        ListNode* result=new ListNode();
        ListNode* cur =head;
        result=cur;
        while(cur!=NULL)
        {
            length++;
            cur=cur->next;
        }
        cur=head;
        if((length-n)==0) return cur->next;
        for(int i=0;i<length-n-1;i++) 
        {
           cur=cur->next;
        }
        cur->next=cur->next->next;
        return result;
    }
};
相关推荐
历程里程碑2 小时前
LeetCode热题11:盛水容器双指针妙解
c语言·数据结构·c++·经验分享·算法·leetcode·职场和发展
wadesir8 小时前
Rust中的条件变量详解(使用Condvar的wait方法实现线程同步)
开发语言·算法·rust
yugi9878389 小时前
基于MATLAB实现协同过滤电影推荐系统
算法·matlab
TimberWill9 小时前
哈希-02-最长连续序列
算法·leetcode·排序算法
Morwit9 小时前
【力扣hot100】64. 最小路径和
c++·算法·leetcode
leoufung9 小时前
LeetCode 373. Find K Pairs with Smallest Sums:从暴力到堆优化的完整思路与踩坑
java·算法·leetcode
wifi chicken10 小时前
数组遍历求值,行遍历和列遍历谁更快
c语言·数据结构·算法
胡楚昊10 小时前
NSSCTF动调题包通关
开发语言·javascript·算法
Gold_Dino11 小时前
agc011_e 题解
算法
bubiyoushang88811 小时前
基于蚁群算法的直流电机PID参数整定 MATLAB 实现
数据结构·算法·matlab