# Multiply a single digit number in place to a number represented as a Linked List

• Difficulty Level : Easy
• Last Updated : 19 Jan, 2022

Given a linked list of N nodes where each node represents digits of a number and a single-digit number M, the task is to multiply the list by M in-place and print the resulting linked list.

Examples:

Input: Linked list: 1 â†’ 2 â†’ 7 â†’ 3 â†’ NULL, M = 3
Output: 3 â†’ 8 â†’ 1 â†’ 9 â†’ NULL
Explanation: The given linked list represents the number 1273. Multiplying 1273 with 3 = 1273*3 = 3819. Hence, the resulting linked list is
3 â†’ 8 â†’ 1 â†’ 9 â†’ NULL

Input: Linked list: 9 â†’ 9 â†’ 9 â†’ NULL, M = 2
Output: 1 â†’ 9 â†’ 9 â†’ 8 â†’ NULL
Explanation: The given linked list represents the number 999. Multiplying 999 with 2 = 999*2 = 1998. Hence, the resulting linked list is
1 â†’ 9 â†’ 9 â†’ 8 â†’ NULL

Approach: Since we are allowed to reverse the LL and add at most 1 extra node to it, the idea to solve this problem is to first reverse the linked list. Then, traverse the linked list and start multiplying M with every node adding the carry generated and updating the carry after each multiplication step. Again, reverse the modified linked list and print the resulting list.

Below is the implementation of the above approach:

## Javascript



Output

3819

Time Complexity: O(N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up
Recommended Articles
Page :