C: input [ [ ] ] . expected output [ ] . How is it possible ?


  • 0
    I

    Coding in C:

    For the input [ [ ] ]
    the expected output is [ ] that means the nestedIterNext() is called atleast once .But it has a return type of int.So I have to return an integer .Not sure how you can get an empty output when an integer is returned.

    It's ugly but it works for all cases but the one mentioned above.

    /**
     * *********************************************************************
     * // This is the interface that allows for creating nested lists.
     * // You should not implement it, or speculate about its implementation
     * *********************************************************************
     *
     * // Return true if this NestedInteger holds a single integer, rather than a nested list.
     * bool NestedIntegerIsInteger(struct NestedInteger *);
     *
     * // Return the single integer that this NestedInteger holds, if it holds a single integer
     * // The result is undefined if this NestedInteger holds a nested list
     * int NestedIntegerGetInteger(struct NestedInteger *);
     *
     * // Return the nested list that this NestedInteger holds, if it holds a nested list
     * // The result is undefined if this NestedInteger holds a single integer
     * struct NestedInteger **NestedIntegerGetList(struct NestedInteger *);
     *
     * // Return the nested list's size that this NestedInteger holds, if it holds a nested list
     * // The result is undefined if this NestedInteger holds a single integer
     * int NestedIntegerGetListSize(struct NestedInteger *);
     * };
     */
    typedef struct NestedIterator {
        struct NestedIterator *next;
        int value;
    };
    
    struct NestedIterator *nestedIterCreate(struct NestedInteger** nestedList, int nestedListSize) {
        struct NestedIterator *curr=NULL,*ret=NULL;
        struct NestedIterator *start=NULL,*prev=NULL;
        for (int i=0;i<nestedListSize;i++){
            if (curr == NULL){
                curr= (struct NestedIterator *)calloc(1,sizeof(struct NestedIterator));
                curr->next=NULL;  
                start=curr;
            }else{
                curr->next=(struct NestedIterator *)calloc(1,sizeof(struct NestedIterator));
                curr->next->next=NULL;
                prev=curr;
                curr=curr->next;
                
            }
            
            if (NestedIntegerIsInteger(nestedList[i])){
                curr->value=NestedIntegerGetInteger(nestedList[i]);
            }else{
                // if it holds a list
                //should return the start of a NestedIterator list
                ret=nestedIterCreate(NestedIntegerGetList(nestedList[i]), NestedIntegerGetListSize(nestedList[i]));
                if (ret==NULL){
                    free(curr);
                    curr=prev;
                    continue;
                }
                curr->value=ret->value;
                curr->next=ret->next;
                free(ret);
                //iterate till end of the added list since curr is always the last node
                while(curr->next->next){
                    curr=curr->next;
                }  
                free(curr->next);//free placeholder of inner list
            }
        }
        if(curr){
            curr->next= (struct NestedIterator *)calloc(1,sizeof(struct NestedIterator));
            curr->next->next=NULL;  
            curr->next->value=-999;//placeholder
        }
        return start;
    }
    
    bool nestedIterHasNext(struct NestedIterator *iter) {
        if (iter){
            return (iter->next!=NULL? 1: 0);
        }
        return 0;
    }
    
    int nestedIterNext(struct NestedIterator *iter) {
            int val=iter->value;
            struct NestedIterator *del=iter->next;
            struct NestedIterator *curr=iter;
            while(del){
                curr->value=del->value;
                curr->next=del->next;
                del=del->next;
                curr=curr->next;
            }
            free(del);
            return val;
    }
    
    /** Deallocates memory previously allocated for the iterator */
    void nestedIterFree(struct NestedIterator *iter) {
        free(iter);
    }
    
    /**
     * Your NestedIterator will be called like this:
     * struct NestedIterator *i = nestedIterCreate(nestedList, nestedListSize);
     * while (nestedIterHasNext(i)) printf("%d\n", nestedIterNext(i));
     * nestedIterFree(i);
     */
    

Log in to reply
 

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