1290. Convert Binary Number in a Linked List to Integer(1ms Runtime).
Anonymous User
28

class Solution {
public int getDecimalValue(ListNode head) {
ArrayList arr=new ArrayList<>();
int c=0,sum=0,val=1;
if(head==null)
return 0;
ListNode temp=head;
while(temp!=null){
int x=temp.val;
arr.add(x);
temp=temp.next;
c++;
}
for(int i=c-1;i>=0;i--){
if(arr.get(i)==1){
sum=sum+val;
}
val=val*2;
}
return sum;
}
}

Comments (0)