FunctionRacerProblemsRace

Plus One Linked List

MediumLinked listsPython, JavaScript, TypeScript, Java, C++

The list holds the digits of a non-negative number with the most significant digit first, like 1 -> 2 -> 9 for 129. Add 1 to the number and return the digits of the result in the same format. Watch out for carries, which can add a new digit at the front. The list is passed in as head.

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

head [1,2,9]
returns [1,3,0]

129 + 1 = 130.

Example 2

head [9,9]
returns [1,0,0]

99 + 1 = 100.

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