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/
No comments:
Post a Comment