给你一个链表,删除链表的倒数第 n
个结点,并且返回链表的头结点。
1.原创
struct ListNode* removeNthFromEnd(struct ListNode* head, int n){
int total,time;
struct ListNode* tmp;
tmp=head;
for(total=1;tmp->next!=NULL;tmp=tmp->next,total++);
total=total-n;
if(total==0)
tmp=head->next;
else if(total>0){
tmp=head;
for(time=1;time!=total;time++){
tmp=tmp->next;
}
tmp->next=tmp->next->next;
tmp=head;
}
else
tmp=NULL;
return tmp;
}
2.看评论用的递归:
class Solution {
public:
int cur=0;
List