1290. Convert Binary Number in a Linked List to Integer

附leetcode链接:https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/
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.

public class ListNode {
       int val;
       ListNode next;
       ListNode(int x) { val = x; }
}
public int getDecimalValue(ListNode head) {
       int value = 0;
       while(head != null) {
            value = (value<<1)|head.val;
            head = head.next;
       }
       return value;
}

小结:二进制链表转化为十进制数字
这里使用了左移一位,并和当前值(按位或 | )
|按位或和&按位与计算方式都是转换二进制再计算,不同的是运算规则(一个为真即为真)1|0 = 1 , 1|1 = 1 , 0|0 = 0 , 0|1 = 1

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1290 Convert Binary Number in a Linked List to Integer 二进...
    air_melt阅读 1,562评论 0 0
  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 13,229评论 0 13
  • NAME dnsmasq - A lightweight DHCP and caching DNS server....
    ximitc阅读 7,969评论 0 0
  • 天气有点闷,中午来上班的时候带了把雨伞。上楼梯的时候碰到楼上的主任,打了一声招呼准备溜之大吉,主任主动开口问了几句...
    圆圆的眼镜阅读 1,706评论 1 0
  • 2月14号,情人节来啦,2020年,注定是不平凡的一年,正月初一,躲在家里,初二,本该带着我生的人去见生我的人,...
    最美少年时阅读 4,898评论 35 33

友情链接更多精彩内容