my 3ms C solution~


  • 0
    S
    struct ListNode* removeNthFromEnd(struct ListNode* head, int n) {
           struct ListNode *cp;
    	struct ListNode *cb;
    	if (head == NULL) return false;
    	struct ListNode *del = (struct ListNode *) calloc(1, sizeof(struct ListNode));
    	if (del == NULL) return false;
    	int count = 0;
    	int num = 0;
    	cb = head;
    	cp = head;
    	while (cb != NULL){
    		++count;
    		cb = cb->next;
    	}
    	num = count - n;
    	if (num == 0) {
    		del = head;
    		head = head->next;
    	}
    	else{
    		while (--num){
    			cp = cp->next;
    		}
    		del = cp->next;
    		cb = cp->next->next;
    		cp->next = cb;
    	}
    	free(del);
    	return head;
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.