1290. Convert Binary Number in a Linked List to Integer

This is a super easy problem. Time Compexity O(n), Space Complexity O(n)

    int res = 0;
    public int getDecimalValue(ListNode head) {
        if(head==null)
            return res;
        res = res*2+head.val;
        return getDecimalValue(head.next);
    }

 

    int res = 0;
    public int getDecimalValue(ListNode head) {
        while(head!=null){
            int val= head.val;
            res= res*2+val;
            head=head.next;
        }
        return res;
    }

 

上一篇:【Linux】NAT模式下关于主机ping不通虚拟机的问题该如何处理


下一篇:PAT 甲 1007 Maximum Subsequence Sum