列表

详情


1290. 二进制链表转整数

给你一个单链表的引用结点 head。链表中每个结点的值不是 0 就是 1。已知此链表是一个整数数字的二进制表示形式。

请你返回该链表所表示数字的 十进制值

 

示例 1:

输入:head = [1,0,1]
输出:5
解释:二进制数 (101) 转化为十进制数 (5)

示例 2:

输入:head = [0]
输出:0

示例 3:

输入:head = [1]
输出:1

示例 4:

输入:head = [1,0,0,1,0,0,1,1,1,0,0,0,0,0,0]
输出:18880

示例 5:

输入:head = [0,0]
输出:0

 

提示:

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} * ListNode(int x, ListNode *next) : val(x), next(next) {} * }; */ class Solution { public: int getDecimalValue(ListNode* head) { } };

rust 解法, 执行用时: 0 ms, 内存消耗: 1.9 MB, 提交时间: 2023-09-15 15:14:31

// Definition for singly-linked list.
// #[derive(PartialEq, Eq, Clone, Debug)]
// pub struct ListNode {
//   pub val: i32,
//   pub next: Option<Box<ListNode>>
// }
// 
// impl ListNode {
//   #[inline]
//   fn new(val: i32) -> Self {
//     ListNode {
//       next: None,
//       val
//     }
//   }
// }
impl Solution {
    pub fn get_decimal_value(head: Option<Box<ListNode>>) -> i32 {
        let mut current = head;
        let mut ret = 0;
        while let Some(mut tmp) = current.take() {
            let next = tmp.next.take();
            ret = ret * 2 + tmp.val;
            current = next;
        }
        ret
    }

    pub fn get_decimal_value2(head: Option<Box<ListNode>>) -> i32 {
        let mut ans = 0;
        let mut cur = head;

        while let Some(node) = cur {
            ans = (ans << 1) | node.val;
            cur = node.next;
        }

        ans
    }
}

cpp 解法, 执行用时: 4 ms, 内存消耗: 8.4 MB, 提交时间: 2023-09-15 15:12:40

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    int getDecimalValue(ListNode* head) {
        int res = 0;
        while ( head ) {
            res <<= 1;
            res += head->val;
            head = head->next;
        }
        return res;
    }
};

java 解法, 执行用时: 0 ms, 内存消耗: 38.9 MB, 提交时间: 2023-09-15 15:11:40

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public int getDecimalValue(ListNode head) {
        int res = 0;
        while ( head != null ) {
            res <<= 1;
            res += head.val;
            head = head.next;
        }
        return res;
    }
}

python3 解法, 执行用时: 32 ms, 内存消耗: 15.8 MB, 提交时间: 2023-09-15 15:11:02

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def getDecimalValue(self, head: ListNode) -> int:
        res = 0
        while head != None:
            res <<= 1
            res += head.val
            head = head.next
        return res

golang 解法, 执行用时: 0 ms, 内存消耗: 2 MB, 提交时间: 2021-05-28 13:55:48

/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func getDecimalValue(head *ListNode) int {
    res := 0
    for head != nil {
        res <<= 1
        res += head.Val
        head = head.Next
    }
    return res
}

php 解法, 执行用时: 4 ms, 内存消耗: 15.1 MB, 提交时间: 2021-05-14 18:28:00

/**
 * Definition for a singly-linked list.
 * class ListNode {
 *     public $val = 0;
 *     public $next = null;
 *     function __construct($val) { $this->val = $val; }
 * }
 */
class Solution {

    /**
     * @param ListNode $head
     * @return Integer
     */
    function getDecimalValue($head) {
        if ( $head == null ) return 0;
        $ans = 0;
        while ( $head != null ) {
            $ans = ( $ans << 1) + $head->val;
            $head = $head->next;
        }
        return $ans;
    }
}

上一题