348. Subtract Two Numbers Represented as Linked Lists

EasyLinked ListLinked ListMathSubtraction

Two non-negative integers are each represented as a linked list of digits. Return the absolute difference as a digit array with no leading zeros (except single 0).

Input: Two linked lists of single digits, head = most significant.

Output: Array of digits of the absolute difference.

Examples

Example 1
Input: [1,2,3], [4,5]
Output: [7,8]
Explanation: 123-45=78 -> [7,8].
Example 2
Input: [9,9,9], [1]
Output: [9,9,8]
Explanation: 999-1=998 -> [9,9,8].
Example 3
Input: [1,2,3], [1,2,3]
Output: [0]
Explanation: 123-123=0 -> [0].

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →