[LeetCode] Convert Binary Number in a Linked List to Integer - JavaScript

이은빈 EUNBIN·2021년 5월 7일
0
post-thumbnail

👩🏻‍💻 문제

LeetCode #1290 Convert Binary Number in a Linked List to Integer



👩🏻‍💻 풀이

var getDecimalValue = function(head) {
    if(!head.next) return head.val;
    
    let binary = "";

    while(head.next) {
        binary += head.val;
        head = head.next;
        if(!head.next) {
            binary += head.val;
        }
    }
    
    return parseInt(binary, 2);
};
// 다른 분의 풀이
let result = 0
    while (head) {
        result = result * 2 + head.val;
        head = head.next
    }
    return result
};
profile
Frontend Engineer & Value Creator

0개의 댓글