二进制计算
-
从左到右算:
例如:0b10011 = 19
public class Demo29 { public static void main(String[] args) { ListNode head = new ListNode(1,new ListNode(0,new ListNode(0,new ListNode(1,new ListNode(1,null))))); System.out.println(getDecimalValue(head)); } public static int getDecimalValue(ListNode head) { ListNode curNode = head; int ans = 0; while (curNode != null) { // 从左边向右边乘 ans = ans * 2 + curNode.val; curNode = curNode.next; } return ans; } } class ListNode { int val; ListNode next; ListNode() { } ListNode(int val, ListNode next) { this.val = val; this.next = next; } }