clueless coding // TODO: be smarter

LeetCode Nested List Weight Sum: Java Solution



TL;DR Code Solution


public int depthSum(List<NestedInteger> nestedList) {

     int sum = 0;
     int level = 1;

     Queue<NestedInteger> queue = new LinkedList<NestedInteger>(nestedList);
     while(!queue.isEmpty()){
         int size = queue.size();

         for(int i = 0; i < size; i++){
             NestedInteger ni = queue.poll();

             if(ni.isInteger()){
                 sum += ni.getInteger() * level;
             }else{
                 queue.addAll(ni.getList());
             }
         }

         level++;
     }

     return sum;


}