Convert Binary Number in a Linked List to Integer
用一个list表示二进制的数, 返回它的十进制表示.
/**
* 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);
}
}