Given head which is a reference node to a singly-linked list. The value of each node in the linked list is either 0 or 1. The linked list holds the binary representation of a number.

Return the decimal value of the number in the linked list.

Example 1:

Input: head = [1,0,1]
Output: 5
Explanation: (101) in base 2 = (5) in base 10

Example 2:

Input: head = [0]
Output: 0

Example 3:

Input: head = [1]
Output: 1

Example 4:

Input: head = [1,0,0,1,0,0,1,1,1,0,0,0,0,0,0]
Output: 18880

Example 5:

Input: head = [0,0]
Output: 0
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {number}
*/
var getDecimalValue = function(head) {
let res = 0; // Traverse linked list
while (head != null) {
// shift bit to accomodate value and add head's data
res = (res << 1) | head.val; // Move next
head = head.next;
}
return res;
};

[Algorithm] 1290. Convert Binary Number in a Linked List to Integer的更多相关文章

  1. 【leetcode】1290. Convert Binary Number in a Linked List to Integer

    题目如下: Given head which is a reference node to a singly-linked list. The value of each node in the li ...

  2. LeetCode 1290. Convert Binary Number in a Linked List to Integer

    题目 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListN ...

  3. LeetCode_405. Convert a Number to Hexadecimal

    405. Convert a Number to Hexadecimal Easy Given an integer, write an algorithm to convert it to hexa ...

  4. [Algorithm] Convert a number from decimal to binary

    125, how to conver to binary number? function DecimalToDinary (n) { let temp = n; let list = []; if ...

  5. [geeksforgeeks] Convert a given Binary Tree to Doubly Linked List

    http://www.geeksforgeeks.org/in-place-convert-a-given-binary-tree-to-doubly-linked-list/ Given a Bin ...

  6. Convert a given Binary Tree to Doubly Linked List

    The question and solution are from: http://www.geeksforgeeks.org/convert-given-binary-tree-doubly-li ...

  7. [LeetCode] Convert a Number to Hexadecimal 数字转为十六进制

    Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s compl ...

  8. Leetcode: Convert a Number to Hexadecimal

    Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two's compl ...

  9. HDU 3711 Binary Number

    Binary Number Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

随机推荐

  1. JDBC简介(一)

    JDBC(Java DataBase Connectivity)是Java与数据库的接口规范,由Java 语言编写的类和接口组成,大致分为两类:针对Java程序员的JDBC API和针对数据库开发商的 ...

  2. 【Excel】删除重复值

  3. "中国东信杯"广西大学第二届程序设计竞赛E Antinomy与红玉海(二分)

    题目大意: n个人,每个人想参加a[i]轮游戏,但每场游戏必须有个一个人当工具人 问最少有几场游戏 题解: 二分 答案范围:[0,sigma a[i]] check:首先a[i]>=ans,其次 ...

  4. nowcoder3274D binary

    题目链接 problem 给定一个01串s,定义rev(x)表示逐位翻转(0变1,1变0)x后并删去前导零后所得到的串.好的串定义如下: s是好的串 如果x是好的串,则rev(x)也是好的串 如果a, ...

  5. 基于ORB-SLAM2的图片识别

    基于ORB-SLAM2的图片识别,其功能是首先运行ORB-SLAM2,在运行过程中调起另一个线程进行图像识别,识别成功后在图片上渲染AR中的立方体模型. 识别过程主要基于ORB-SLAM2中的BoW算 ...

  6. SQLServer某个库log日志过大,无法收缩日志文件 ,因为该文件结尾的逻辑日志文件正在使用

    问题描述: 今天看到user库日志备份方面很久,然后查看到user库这个log日志很大 图片是我已经解决了,然后现在可以收缩的大小 解决方法: 1.先备份user库日志,因为很大,所以要等很久,这个只 ...

  7. 《细说PHP》第四版 样章 第18章 数据库抽象层PDO 5

    18.5  使用PDO对象 PDO扩展类库为PHP访问数据库定义了一个轻量级.一致性的接口,它提供了一个数据访问抽象层,这样,无论使用什么数据库,都可以通过一致的函数执行查询和获取数据,大大简化了数据 ...

  8. IT兄弟连 Java语法教程 流程控制语句 循环结构语句2

    双重for循环 如果把一个循环放在另一个循环体中,那么就可以形成嵌套循环,也就是双重for循环,当然嵌套循环也可以是for循环嵌套while循环,也可以是while循环嵌套while循环……,即各种类 ...

  9. Leetcode练习题Longest Common Prefix

    Question: Longest Common Prefix Write a function to find the longest common prefix string amongst an ...

  10. 使用openpyxl模块进行封装,高效处理excel测试数据

    from openpyxl import load_workbook from scripts.handle_config import conf from scripts.constants imp ...