解题思路:
参考了leetcode里面的解法,采用双指针固定长度浮窗方案(不知道命名是否准确),即设立两个指针,起始指针p,以及结束指针q,p先保持不动,q移动到n+1的位置,然后p,q同时往后移动,直至q达到NULL点。
struct ListNode* removeNthFromEnd(struct ListNode* head, int n){
struct ListNode *res = (struct ListNode*)malloc (sizeof(struct ListNode));
res -> next = head;
struct ListNode *p = res;
struct ListNode *q = res;
for (int i = 0; i < n+1; i++ ){
q = q -> next;
}
while (q){
p = p -> next;
q = q -> next;
}
p-> next = p -> next -> next;
return res->next;
}