[Algorithm] 1290. Convert Binary Number in a Linked List to Integer
List to in number Binary ALGORITHM Integer convert
2023-09-14 08:59:14 时间
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; };
相关文章
- Performance comparison for loops of List in java
- [React] Refactor a Stateful List Component to a Functional Component with React PowerPlug
- 【RF库Collections测试】Count Values In List
- GVM 21.4.3 Failed to find port_list '33d0cd82-57c6-11e1-8ed1-406186ea4fc5'
- Scala入门到精通——第三节 Array、List
- [Algorithm] 234. Palindrome Linked List / Reverse linked list
- Python3 数据结构:列表List中的方法
- How to render S4 Code List extension field into CRM WebUI product search view
- atitit js 开发工具 ide的代码结构显示(func list) outline总结
- DHTMLX JavaScript 待办事项+To Do List
- Android报错提示Attempt to invoke interface method ‘int java.util.List.size()‘ on a null object reference
- 成功解决KeyError: “Passing list-likes to .loc or [] with any missing labels is no longer supported. The
- 已解决FutureWarning: The default value of regex will change from True to False in a future version. In
- java 从一个总的list集合中,去掉指定的集合元素,得到新的集合——removeAll()
- Sqlachemy的警告SAWarning: The IN-predicate on "sns_object.BIZ_ID" was invoked with an empty sequence. This results in a contradiction, which nonetheless can be expensive to evaluate.
- littlevgl之list 列表控件
- leetcode dfs Flatten Binary Tree to Linked List
- [to do list][PCB][questions]and[plan]
- drop down list for little vGL
- List<?> list= new ArrayList<?>接口引用指向实现类的对象.
- 解决SVN无法add to ignore list的问题