You are given the head of a non-empty linked list whose nodes hold the digits of a non-negative integer, most significant digit first, with no leading zeros except for the number 0 itself. Return the head of a linked list holding the digits of the number after doubling it.
You are given the head of a non-empty linked list whose nodes hold the digits of a non-negative integer, most significant digit first, with no leading zeros except for the number 0 itself. Return the head of a linked list holding the digits of the number after doubling it.
Example 1:
Input: {"head":[1,8,9]}
Output: [3,7,8]
Constraints:
The number of nodes in the list is in the range [1,104].
0≤Node.val≤9
The input has no leading zeros, except for the number 0 itself.
Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result
Run code to see results.
Input
arr ={"head":[1,8,9]}
The list spells 189. Doubling it digit by digit only needs one look ahead: a digit gets +1 when the next digit is 5 or more, because that digit's double spills a carry.