Plus One 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**. The number has no leading zeros, except for the number 0 itself, which is the single node [0]. Add one to the number and return the head of the list representing the result, in the same format. The result may need one more node than the input (for example when every digit is 9).

Examples

Input: head = [1,2,3]
Output: [1,2,4]
123 + 1 = 124.
Input: head = [9,9]
Output: [1,0,0]
99 + 1 = 100, which needs a new leading node.
Input: head = [0]
Output: [1]
0 + 1 = 1.

Constraints

  • The number of nodes is in `[1, 100]`.
  • `0 <= Node.val <= 9`
  • The number 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