leetcode

Leetcode submissions
git clone git://git.laack.co/leetcode.git
Log | Files | Refs | README

convert-binary-number-in-a-linked-list-to-integer.dart (801B)


      1 //Given a linked list return the decimal form of the binary value.
      2 //To do this I placed all of the values into an array list. Then
      3 //I iterated through the list in reverse order and if the value was a 1
      4 //at the given index I would add 2^n where n is the current node.
      5 //The time complexity of this code is O(n) where n is the number of nodes
      6 //in the list.
      7 //Time: 240ms Beats: 87.50%
      8 //Memory: 142.3MB Beats: 62.50%
      9 
     10 class Solution {
     11   int getDecimalValue(ListNode? head) {
     12     List<int> vals = [];
     13     while(head != null){
     14       vals.add(head.val);
     15       head = head.next;
     16     }
     17     int return_val = 0;
     18     int itr = 0;
     19     for(int i = vals.length -1 ; i >= 0 ; --i){
     20       if(vals[i] == 1){
     21         return_val += (pow(2 , itr)).toInt();
     22       }
     23       itr += 1;
     24     }
     25     return return_val;
     26   }
     27 }