Linked List Product of Nodes Between 0s
Last Updated :
07 Sep, 2022
Given a linked list that contains a series of numbers separated by “0”. Multiply them and store them in the linked list in place.
Note: There will not be continuous zeros in the input.
Examples:
Input : 1->2->3->0->5->4->0->3->2->0
Output : 6->20->6
Input : 1->2->3->4
Output : 1->2->3->4
Approach:
- Start iterating over nodes of the linked list.
- Iterate while temp.data !=0, and multiply these data into a variable ‘prod’.
- When you encounter 0 as the node’s data, change pointers of previous nodes.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
struct Node
{
int data;
Node* next;
Node( int d)
{
data = d;
next = NULL;
}
};
void printList(Node* head)
{
while (head->next != NULL)
{
cout<<head->data << "-> " ;
head = head->next;
}
cout << head->data << "\n" ;
}
void inPlaceStore(Node* head)
{
if (head->data == 0)
{
head = head->next;
}
Node* res = head;
Node* temp = head;
int prod = 1;
while (temp != NULL)
{
if (temp->data != 0)
{
prod *= temp->data;
temp = temp->next;
}
else
{
res->data = prod;
res->next = temp->next;
temp = temp->next;
res = res->next;
prod = 1;
}
}
res->data = prod;
res->next = temp;
printList(head);
}
int main()
{
Node *head = new Node(3);
head->next = new Node(2);
head->next->next = new Node(0);
head->next->next->next = new Node(4);
head->next->next->next->next = new Node(5);
head->next->next->next->next->next = new Node(0);
head->next->next->next->next->next->next = new Node(6);
head->next->next->next->next->next->next->next = new Node(7);
inPlaceStore(head);
return 0;
}
|
Java
class Node {
int data;
Node next;
public Node( int data)
{
this .data = data;
this .next = null ;
}
}
public class inPlaceStoreLL {
static void inPlaceStore(Node head)
{
if (head.data == 0 ) {
head = head.next;
}
Node res = head;
Node temp = head;
int prod = 1 ;
while (temp != null ) {
if (temp.data != 0 ) {
prod *= temp.data;
temp = temp.next;
}
else {
res.data = prod;
res.next = temp.next;
temp = temp.next;
res = res.next;
prod = 1 ;
}
}
res.data = prod;
res.next = temp;
printList(head);
}
static void printList(Node head)
{
while (head.next != null ) {
System.out.print(head.data + "-> " );
head = head.next;
}
System.out.println(head.data);
}
public static void main(String[] args)
{
Node head = new Node( 3 );
head.next = new Node( 2 );
head.next.next = new Node( 0 );
head.next.next.next = new Node( 4 );
head.next.next.next.next = new Node( 5 );
head.next.next.next.next.next = new Node( 0 );
head.next.next.next.next.next.next = new Node( 6 );
head.next.next.next.next.next.next.next = new Node( 7 );
inPlaceStore(head);
}
}
|
Python3
import math
class Node:
def __init__( self , data):
self .data = data
self . next = None
def printList(head):
while (head. next ! = None ):
print (head.data, end = "->" )
head = head. next
print (head.data)
print ()
def inPlaceStore(head):
if (head.data = = 0 ):
head = head. next
res = head
temp = head
prod = 1
while (temp ! = None ):
if (temp.data ! = 0 ):
prod = prod * temp.data
temp = temp. next
else :
res.data = prod
res. next = temp. next
temp = temp. next
res = res. next
prod = 1
res.data = prod
res. next = temp
printList(head)
if __name__ = = '__main__' :
head = Node( 3 )
head. next = Node( 2 )
head. next . next = Node( 0 )
head. next . next . next = Node( 4 )
head. next . next . next . next = Node( 5 )
head. next . next . next . next . next = Node( 0 )
head. next . next . next . next . next . next = Node( 6 )
head. next . next . next . next . next . next . next = Node( 7 )
inPlaceStore(head)
|
C#
using System;
public class Node
{
public int data;
public Node next;
public Node( int data)
{
this .data = data;
this .next = null ;
}
}
public class inPlaceStoreLL
{
static void inPlaceStore(Node head)
{
if (head.data == 0)
{
head = head.next;
}
Node res = head;
Node temp = head;
int prod = 1;
while (temp != null )
{
if (temp.data != 0)
{
prod *= temp.data;
temp = temp.next;
}
else
{
res.data = prod;
res.next = temp.next;
temp = temp.next;
res = res.next;
prod = 1;
}
}
res.data = prod;
res.next = temp;
printList(head);
}
static void printList(Node head)
{
while (head.next != null )
{
Console.Write(head.data + "-> " );
head = head.next;
}
Console.WriteLine(head.data);
}
public static void Main(String[] args)
{
Node head = new Node(3);
head.next = new Node(2);
head.next.next = new Node(0);
head.next.next.next = new Node(4);
head.next.next.next.next = new Node(5);
head.next.next.next.next.next = new Node(0);
head.next.next.next.next.next.next = new Node(6);
head.next.next.next.next.next.next.next = new Node(7);
inPlaceStore(head);
}
}
|
Javascript
<script>
class Node {
constructor(data) {
this .data = data;
this .next = null ;
}
}
function inPlaceStore(head) {
if (head.data == 0) {
head = head.next;
}
var res = head;
var temp = head;
var prod = 1;
while (temp != null ) {
if (temp.data != 0) {
prod *= temp.data;
temp = temp.next;
}
else {
res.data = prod;
res.next = temp.next;
temp = temp.next;
res = res.next;
prod = 1;
}
}
res.data = prod;
res.next = temp;
printList(head);
}
function printList(head) {
while (head.next != null ) {
document.write(head.data + "-> " );
head = head.next;
}
document.write(head.data);
}
var head = new Node(3);
head.next = new Node(2);
head.next.next = new Node(0);
head.next.next.next = new Node(4);
head.next.next.next.next = new Node(5);
head.next.next.next.next.next = new Node(0);
head.next.next.next.next.next.next = new Node(6);
head.next.next.next.next.next.next.next = new Node(7);
inPlaceStore(head);
</script>
|
Share your thoughts in the comments
Please Login to comment...