Multiply a single digit number in place to a number represented as a Linked List
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:
C++14
#include <bits/stdc++.h>
using namespace std;
class Node {
public :
int data;
Node* next;
};
Node* newNode( int data)
{
Node* new_node = new Node;
new_node->data = data;
new_node->next = NULL;
return new_node;
}
Node* reverse(Node* head)
{
Node* prev = NULL;
Node* current = head;
Node* next;
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
return prev;
}
Node* multiplyHelp(Node* head, int M)
{
Node* res = head;
Node* prev = NULL;
int carry = 0, product = 1;
while (head != NULL) {
product = head->data * M;
product += carry;
carry = product / 10;
head->data = product % 10;
prev = head;
head = head->next;
}
if (carry > 0)
prev->next = newNode(carry);
return res;
}
Node* multiply(Node* head, int M)
{
head = reverse(head);
head = multiplyHelp(head, M);
return reverse(head);
}
void printList(Node* node)
{
while (node != NULL) {
cout << node->data;
node = node->next;
}
}
int main()
{
Node* head = newNode(1);
head->next = newNode(2);
head->next->next = newNode(7);
head->next->next->next = newNode(3);
int M = 3;
head = multiply(head, M);
printList(head);
return 0;
}
|
Java
import java.util.*;
class GFG {
static class Node {
int data;
Node next;
};
static Node newNode( int data)
{
Node new_node = new Node();
new_node.data = data;
new_node.next = null ;
return new_node;
}
static Node reverse(Node head) {
Node prev = null ;
Node current = head;
Node next;
while (current != null ) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
return prev;
}
static Node multiplyHelp(Node head, int M) {
Node res = head;
Node prev = null ;
int carry = 0 , product = 1 ;
while (head != null ) {
product = head.data * M;
product += carry;
carry = product / 10 ;
head.data = product % 10 ;
prev = head;
head = head.next;
}
if (carry > 0 )
prev.next = newNode(carry);
return res;
}
static Node multiply(Node head, int M)
{
head = reverse(head);
head = multiplyHelp(head, M);
return reverse(head);
}
static void printList(Node node) {
while (node != null ) {
System.out.print(node.data);
node = node.next;
}
}
public static void main(String[] args)
{
Node head = newNode( 1 );
head.next = newNode( 2 );
head.next.next = newNode( 7 );
head.next.next.next = newNode( 3 );
int M = 3 ;
head = multiply(head, M);
printList(head);
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data;
self . next = None ;
def newNode(data):
new_Node = Node(data);
return new_Node;
def reverse(head):
prev = None ;
current = head;
next = None ;
while (current ! = None ):
next = current. next ;
current. next = prev;
prev = current;
current = next ;
return prev;
def multiplyHelp(head, M):
res = head;
prev = None ;
carry = 0 ;
product = 1 ;
while (head ! = None ):
product = head.data * M;
product + = carry;
carry = product / / 10 ;
head.data = product % 10 ;
prev = head;
head = head. next ;
if (carry > 0 ):
prev. next = newNode(carry);
return res;
def multiply(head, M):
head = reverse(head);
head = multiplyHelp(head, M);
return reverse(head);
def printList(node):
while (node ! = None ):
print (node.data,end = "");
node = node. next ;
if __name__ = = '__main__' :
head = newNode( 1 );
head. next = newNode( 2 );
head. next . next = newNode( 7 );
head. next . next . next = newNode( 3 );
M = 3 ;
head = multiply(head, M);
printList(head);
|
C#
using System;
public class GFG {
class Node {
public int data;
public Node next;
};
static Node newNode( int data)
{
Node new_node = new Node();
new_node.data = data;
new_node.next = null ;
return new_node;
}
static Node reverse(Node head) {
Node prev = null ;
Node current = head;
Node next;
while (current != null ) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
return prev;
}
static Node multiplyHelp(Node head, int M) {
Node res = head;
Node prev = null ;
int carry = 0, product = 1;
while (head != null ) {
product = head.data * M;
product += carry;
carry = product / 10;
head.data = product % 10;
prev = head;
head = head.next;
}
if (carry > 0)
prev.next = newNode(carry);
return res;
}
static Node multiply(Node head, int M)
{
head = reverse(head);
head = multiplyHelp(head, M);
return reverse(head);
}
static void printList(Node node) {
while (node != null ) {
Console.Write(node.data);
node = node.next;
}
}
public static void Main(String[] args)
{
Node head = newNode(1);
head.next = newNode(2);
head.next.next = newNode(7);
head.next.next.next = newNode(3);
int M = 3;
head = multiply(head, M);
printList(head);
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .next = null ;
}
}
function newNode(data)
{
var new_node = new Node();
new_node.data = data;
new_node.next = null ;
return new_node;
}
function reverse(head) {
var prev = null ;
var current = head;
var next;
while (current != null ) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
return prev;
}
function multiplyHelp(head , M) {
var res = head;
var prev = null ;
var carry = 0, product = 1;
while (head != null ) {
product = head.data * M;
product += carry;
carry = parseInt(product / 10);
head.data = product % 10;
prev = head;
head = head.next;
}
if (carry > 0)
prev.next = newNode(carry);
return res;
}
function multiply(head , M)
{
head = reverse(head);
head = multiplyHelp(head, M);
return reverse(head);
}
function printList(node) {
while (node != null ) {
document.write(node.data);
node = node.next;
}
}
var head = newNode(1);
head.next = newNode(2);
head.next.next = newNode(7);
head.next.next.next = newNode(3);
var M = 3;
head = multiply(head, M);
printList(head);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...