/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */classSolution {public:ListNode*removeNthFromEnd(ListNode* head,int n) { ListNode* dummy =newListNode(0);dummy->next = head; ListNode* slow = dummy; ListNode* fast = dummy; // Let fast go n+1 step forward // (when fast reach NULL, slow will be the node before the len-n th one)for(int i=0; i<n+1; i++) { fast =fast->next; } // Move slow and fast together until fast is at the tailwhile(fast) { fast =fast->next; slow =slow->next; }slow->next =slow->next->next;returndummy->next; }};