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. LeetCode 1255 得分最高的单词集合 Maximum Score Words Formed by Letters

    地址 https://leetcode-cn.com/problems/maximum-score-words-formed-by-letters/ 题目描述你将会得到一份单词表 words,一个字母 ...

  2. 数据嵌入js的关系图

    参照echarts官网,改了一下效果图: 数据放在了js里. 代码: <%@ page language="java" contentType="text/html ...

  3. vue-cil3 运行报错 --- warnings potentially fixable with the `--fix` option

    warnings potentially fixable with the `--fix` option. 将一下部分:"lint": "vue-cli-service ...

  4. java(二)变量

    基础数据类型: 数值型:整型(byte.short.int.long).浮点型(float.double)java各整数类型有固定的表数范围和字段长度,不受具体os的影响,以保持java的可移植性:j ...

  5. dotnetcore实现Aop

    dotnetcore实现Aop Aop大家都不陌生,然而今天给大家不将讲官方的filter,今天给大家分享一个轻量级的Aop解决方案(AspectCore) 什么是AspectCore AspectC ...

  6. 接口测试用例yaml格式数据

    1. login.yaml yaml文件 - name # 添加减号可以把用例转为list,每一部分是一个字典 url: /api/user/login method: post data: # 存放 ...

  7. Unity TextMeshPro替代Text组件创建简体中文字体纹理集

    Unity原生的Text组件有一个毛病,只要文本放大字体放大就会有毛边或锯齿,一个更好的解决方案是用TextMeshPro替代ugui中的Text组件. TMPro采用SDF文字渲染技术,可以使文字放 ...

  8. 【Linux】LVM操作添加新硬盘

    目录 1.查看当前硬盘及分区情况 2.初始化/dev/sdb为PV(physical volume) 3.PV加入至VG组. 4.创建lv 5.格式化逻辑分区 6.挂载硬盘/data 7.迁移zabb ...

  9. 关于 ASP.NET Core 中的 OData

    1. BooksController using BooksODataService.Models; using Microsoft.AspNet.OData; using Microsoft.Asp ...

  10. ojdbc.jar在maven中报错(下载不了)

    一.问题 由于oracle的版权问题,java连接oracle的jar(ojdbc.jar)在maven的中央仓库下载不到,然后导致maven项目报错. 二.解决 第一步:下载ojdbc.jar 由于 ...