Skip to content

Latest commit

 

History

History
executable file
·
32 lines (22 loc) · 623 Bytes

File metadata and controls

executable file
·
32 lines (22 loc) · 623 Bytes

1290. 二进制链表转整数

https://leetcode-cn.com/problems/convert-binary-number-in-a-linked-list-to-integer

Solution

Swift

func getDecimalValue(_ head: ListNode?) -> Int {
    
    var current: ListNode? = nil
    current = head
    
    var decimals = [Int]()
    while current != nil {
        decimals.append(current!.val)
        current = current?.next
    }
    
    var output = 0
    for index in 0..<decimals.count {
        let y = Double(decimals.count-1-index)
        let x = Double(2)
        output = output + decimals[index] * Int(pow(x, y))
    }
    
    return output
}