/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */
//一开始发现头结点不好删啊,然后加了一个前置节点就ok了
class Solution { public: ListNode* removeNthFromEnd(ListNode* head, int n) { ListNode *add = new ListNode(0); add->next = head; ListNode *end = head; ListNode *start = add; for(int i=1;i < n;i++){ end = end->next; } while(end->next != NULL){ end = end->next; start = start->next; } start->next = start->next->next; return add->next; } };