How to optimization this code?


  • -1
    X

    if(head == NULL || n == 0)
    return head;
    ListNode* hopeEnd = head;
    int count = 0;
    while(hopeEnd){
    ++count;
    hopeEnd = hopeEnd->next;
    }
    int pos = count - n;
    hopeEnd = head;
    if(pos == 0){
    head = head->next;
    delete hopeEnd;
    }else if(pos > 0){
    pos = pos - 1;
    while(pos-- > 0){
    hopeEnd = hopeEnd->next;
    }
    ListNode* tmp = hopeEnd->next;
    hopeEnd->next = tmp->next;
    delete tmp;
    }
    return head;


Log in to reply
 

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