public class Solution {
public int GetDecimalValue(ListNode head) {
var stack = new Stack<int>();
while (head != null) {
stack.Push(head.val);
head = head.next;
}
int n = 1;
int sum = 0;
foreach (var digit in stack) {
sum += digit * n;
n *= 2;
}
return sum;
}
}
Source: https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/
"Simplicity can't be bought later, it must be earned from the start" -- DB
Saturday, February 20, 2021
Leetcode Everyday: 1290. Convert Binary Number in a Linked List to Integer
Labels:
leetcode
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment