Convert Binary Number in a Linked List to Integer
用一个list表示二进制的数, 返回它的十进制表示.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ class Solution { public int getDecimalValue(ListNode head) { StringBuilder sb = new StringBuilder(); while(head != null) { sb.append(head.val); head = head.next; } return Integer.parseInt(sb.toString(), 2); } } |