1290. Convert Binary Number in a Linked List to Integer

Posted johnnyzhao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1290. Convert Binary Number in a Linked List to Integer相关的知识,希望对你有一定的参考价值。

package LeetCode_1290

/**
 * 1290. Convert Binary Number in a Linked List to Integer
 * https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/description/
 * */

class ListNode(var `val`: Int) {
    var next: ListNode? = null
}

class Solution {
    fun getDecimalValue(head_: ListNode?): Int {
        var result = 0
        var len = 0.0
        var node = head_
        var head = head_
        while (node != null) {
            len++
            node = node.next
        }
        len--
        //binary to decimal by math formula: (base two) 101 = 1*2^2 + 0*2^1 + 1*2^0
        while (head != null) {
            result += head.`val` * Math.pow(2.0, len--).toInt()
            head = head.next
        }
        return result
    }
}

 

以上是关于1290. Convert Binary Number in a Linked List to Integer的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode --- 1290. Convert Binary Number in a Linked List to Integer 解题报告

1290. 二进制链表转整数

算法leetcode1290. 二进制链表转整数(多语言实现)

算法leetcode1290. 二进制链表转整数(多语言实现)

Convert Sorted Array to Binary Search Tree & Convert Sorted List to Binary Search Tree

Convert Sorted Array to Binary Search Tree