Saturday, November 29, 2014

Remove Nth Node From End of List

ListNode *removeNthFromEnd(ListNode *head, int n) {
    ListNode dummy(0), *back = &dummy, *front = &dummy;
    dummy.next = head;
    while (n--) front = front->next;
    while (front->next) {
        front = front->next;
        back = back->next;
    }
    ListNode *del = back->next;
    back->next = del->next;
    delete del;
    return dummy.next;
}

No comments:

Post a Comment