点击打开链接
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string>
#include<cstring>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#include<limits.h>
#include<assert.h>
#define LL long long
#define inf 0x3f3f3f3f
#define mod 1e9+7
using namespace std;
struct ListNode{
int val;
ListNode *next;
ListNode(int x):val(x),next(NULL){}
};
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
int cnt=0;
ListNode* node =head;
while (node) {
++cnt;
node=node->next;
}
int index=cnt-n;
if(index==0){
head=head->next;
}else{
node=head;
while (index--) {
node=node->next;
}
node->next = node->next->next;//删除点
}
return head;
}
};
int main()
{
Solution temple;
return 0;
}