FunctionRacerProblemsRace

Multiply Two Numbers

HardLinked listsPython, JavaScript, TypeScript, Java, C++

Two whole numbers, 0 or larger, are stored as linked lists with one digit per node, the leading digit first. So 1 -> 2 -> 3 is the number 123. Neither number starts with a 0, unless it is the number 0 itself.

Return their product as a linked list in the same form. The numbers can be far too long for an ordinary number type in some languages, so work with the digits. The lists are passed in as first and second.

Linked lists appear in the tests as the list of their values, like [1,2,3]. Each node is a ListNode with .val and .next; the class is already defined for you.

Example 1

first [1,2]
second [3,4]
returns [4,0,8]

12 times 34 is 408.

Example 2

first [0]
second [5,7]
returns [0]

Anything times 0 is 0, written as one node.

Example 3

first [9,9]
second [9,9]
returns [9,8,0,1]

99 times 99 is 9801.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Linked lists problems