http://oj.leetcode.com/problems/remove-nth-node-from-end-of-list/
/** * 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 *current=head, *advance=head; for(int i=0;i<n;i++) advance=advance->next; // It is very important to take care of the case where the head node should be deleted if(advance==NULL){ ListNode* NextOne = head->next; delete head; return NextOne; } while(advance->next!=NULL){ current=current->next; advance=advance->next; } ListNode* NextOne = current->next->next; delete current->next; current->next=NextOne; return head; } };