Double a Number Represented as a Linked List

MediumLinked Listmath

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

A non-negative integer is stored in a linked list, one decimal digit per node, with the **most significant digit at the head**. There are no leading zeros except for the number 0 itself (the single node [0]). Return the head of a linked list, in the same format, representing **twice** the number. The result may be one node longer than the input. The list can be much longer than any built-in integer type, so work digit by digit.

Examples

Input: head = [1,8,9]
Output: [3,7,8]
189 * 2 = 378.
Input: head = [9,9,9]
Output: [1,9,9,8]
999 * 2 = 1998, which gains a leading digit.
Input: head = [0]
Output: [0]
0 * 2 = 0.

Constraints

  • The number of nodes is in `[1, 10^4]`.
  • `0 <= Node.val <= 9`
  • The input has no leading zeros except the number `0` itself.

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More math problems