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;
    }

 

posted on 2022-02-15 05:02  阳光明媚的菲越  阅读(17)  评论(0编辑  收藏  举报