LeetCode 1290

128 阅读1分钟

leetcode-cn.com/problems/co…

import java.util.Stack;

/**
 * Created by yanyongjun on 2020/2/14.
 */
class Solution {
    public int getDecimalValue(ListNode head) {
        if (head == null) {
            return 0;
        }
        Stack<Integer> stack = new Stack<>();
        stack.push(head.val);
        while (head.next != null) {
            head = head.next;
            stack.push(head.val);
        }
        int result = 0;
        int cur = 1;
        while (!stack.isEmpty()) {
            int tmp = stack.pop();
            result = cur * tmp + result;
            cur = cur * 2;
        }
        return result;
    }
}