DEV Community

Karleb
Karleb

Posted on

#2816. Double a Number Represented as a Linked List

https://leetcode.com/problems/double-a-number-represented-as-a-linked-list/description/?envType=daily-question&envId=2024-05-07

/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var doubleIt = function(head) {
    const vals = []
    let curr = head

    while (curr) {
        vals.push(curr.val)
        curr = curr.next
    }

    let newVals = BigInt(vals.join('')) * 2n
    newVals = String(newVals).split('')


    let dummy = new ListNode()
    let dummyCurr = dummy

    for (const val of newVals) {
        dummyCurr.next = new ListNode(val)
        dummyCurr = dummyCurr.next
    }

    return dummy.next
};

Enter fullscreen mode Exit fullscreen mode

Top comments (0)