[Algorithm] 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 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 10Example 2:
Input: head = [0]
Output: 0Example 3:
Input: head = [1]
Output: 1Example 4:
Input: head = [1,0,0,1,0,0,1,1,1,0,0,0,0,0,0]
Output: 18880Example 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的更多相关文章
- 【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 ...
- LeetCode 1290. Convert Binary Number in a Linked List to Integer
题目 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListN ...
- 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 ...
- [Algorithm] Convert a number from decimal to binary
125, how to conver to binary number? function DecimalToDinary (n) { let temp = n; let list = []; if ...
- [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 ...
- 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 ...
- [LeetCode] Convert a Number to Hexadecimal 数字转为十六进制
Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s compl ...
- Leetcode: Convert a Number to Hexadecimal
Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two's compl ...
- HDU 3711 Binary Number
Binary Number Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
随机推荐
- 剑指Offer-31.整数中1出现的次数(从1到n整数中1出现的次数)(C++/Java)
题目: 求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1.10.11.12.13因此共出现6次,但是对于后面问题他就没辙了.A ...
- hashlib和hmac模块
目录 一.hashlib模块 1.0.1 hash是什么 1.0.2 撞库破解hash算法加密 一.hashlib模块 1.0.1 hash是什么 hash是一种算法(Python3.版本里使用has ...
- 用Fastclick解决移动端300ms延迟问题
移动设备上的浏览器默认会在用户点击屏幕大约延迟300毫秒后才会触发点击事件,这是为了检查用户是否在做双击. 为了能够立即响应用户的点击事件,才有了FastClick. 用法: 引入fastclick. ...
- IT兄弟连 Java语法教程 数组 数组的初始化
Java语言中数组必须先初始化,然后才可以使用.所谓初始化,就是为数组的数组元素分配内存空间,并为每个数组元素赋初始值. 这时有人会问,能不能只分配内存空间,不赋初始值呢?答案是肯定不行的,一旦为数组 ...
- linux的vi编辑器常用用法一览
vi 命令用于编辑文本文件,语法: vi 文件名 vi 是一个比较强大的编辑工具,类似于windows下的notepad,但是功能要强大的多.vi分为三种模式,分别是“一般模式”,“编辑模式”,“命令 ...
- sierpinski地毯
(分形作业) 取一矩形,九等分而去其中. 每一份九等分去其中:循环往复. 方法一(传统方法) 将每个矩形映射到三个矩形中去即可. def big(a,times): k=3**tim ...
- 黄聪:PHP转换网址相对路径到绝对路径的一种方法
相信很多程序(尤其是采集类的程序)都会有需要把网址的相对路径转换成绝对路径的需要,例如采集到某页面的HTML代码中包含资源文件经常会看到这样的文件名: <link rel="style ...
- MongoDB for OPS 03:分片 shard 集群
写在前面的话 上一节的复制集也就是主从能够解决我们高可用和数据安全性问题,但是无法解决我们的性能瓶颈问题.所以针对性能瓶颈,我们需要采用分布式架构,也就是分片集群,sharding cluster! ...
- oracle学习笔记(十九) 子程序——存储过程
子程序--存储过程 我们可以使用子程序来封装一下我们需要的操作,子程序又有存储过程,函数和触发器. 这里先学习存储过程~ 语法 create [or replace] procedure $proce ...
- HTTP 状态码及含义
来自 Koa.js 官方文档中关于设置请求响应的部分 response.status=,列出了从 1xx ~ 5xx HTTP 状态码及含义,现摘录如下: 100 "continue&quo ...