Easy to understand 8 lines c/c++ code


  • 0
    G
    ListNode* removeNthFromEnd(ListNode* head, int n) {
            ListNode* temp=head,*prev=head;
            for(int i=0;i<n;i++) {
                if(temp==NULL)return head;
                temp=temp->next;
            }
            if(temp==NULL) return head->next;
            while(temp->next!=NULL)prev=prev->next,temp=temp->next;
            prev->next=prev->next->next;
            return head;
        }

Log in to reply
 

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