https://leetcode.com/problems/remove-nth-node-from-end-of-list/
删掉链表的倒数第n个节点
想着什么空间换时间 记录什么的。。。真的很蠢。。。。
代码一看便懂
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode *p1 = head, *p2 = head;
while(n--){
p1 = p1->next;
}
if(p1 == NULL){
return head->next;
}
while(p1->next != NULL){
p1 = p1->next;
p2 = p2->next;
}
p2->next = p2->next->next;
return head;
}
};