leetcode19Remove Nth Node From End of List
思路:
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
int length = 0;
ListNode *h1 = head;
ListNode *tempHead = new ListNode(0);
while(h1!=NULL) {length++;h1 = h1->next;}
int nthFromStart = length - n + 1;
tempHead->next = head;
head = tempHead;
h1 = head;
while(nthFromStart-->1) h1 = h1->next;
h1->next = h1->next->next;
head = head->next;
return head;
}
};