1. 程式人生 > >Leetcode: Remove Nth Node From End of List

Leetcode: Remove Nth Node From End of List

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* removeNthFromEnd(struct ListNode* head, int n) {
   struct ListNode *first=head;
struct    ListNode *end=head;
    while(n--){
        end=end->next;
    }
    if(end==NULL){
        return head->next;
    }
    while(end->next!=NULL){
        first=first->next;
        end=end->next;
    }
    first->next=first->next->next;
    return head;
}