给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
int len=0;
ListNode* newOne = new ListNode(0,head);
while(head)
{
head = head->next;
len++;
}
int m = len - n +1;
ListNode* cur = newOne;
for(int i=1 ; i<m ; i++)
{
cur = cur->next;
}
cur->next = cur->next->next;
return newOne->next;
}
};