Linked List Product of Nodes Between 0s

Given a linked list which contains series of numbers seperated by “0”. Product them and store in the linked list in-place.

Note :There will not be continuous zeros in 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:

  1. Start iterating over nodes of linked list.
  2. Iterate while temp.data !=0, and product these data into a variable ‘prod’.
  3. When you encounter 0 as the node’s data, change pointers of previous nodes.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to in-place product linked list
// nodes between 0s
#include<bits/stdc++.h>
using namespace std;
  
// Linked List Node
struct Node 
{
    int data;
    Node* next;
    Node(int d)
    {
        data = d;
        next = NULL;
    }
};
  
// Function to traverse and print Linked List
    void printList(Node* head)
    {
        while (head->next != NULL) 
        {
            cout<<head->data <<"-> ";
                head = head->next;
        }
        cout << head->data << "\n";
    }
  
// Function to in-place product linked list
// nodes between 0s
  
    // Function to store numbers till 0
    void inPlaceStore(Node* head)
    {
        if (head->data == 0) 
        {
            head = head->next;
        }
  
        // To store modified list
        Node* res = head;
  
        // Traverse linked list and keep
        // adding nodes between 0s.
        Node* temp = head;
        int prod = 1;
        while (temp != NULL)
        {
  
            // loop to product the data of nodes till
            // it encounters 0
            if (temp->data != 0)
            {
                prod *= temp->data;
                temp = temp->next;
            }
  
            // If we encounters 0, we need
            // to update next pointers
            else
            {
                  
                res->data = prod;
                res->next = temp->next;
                temp = temp->next;
                res = res->next;
                prod = 1;
            }
        }
          
        // For the last segment
        res->data = prod;
        res->next = temp;
                  
        printList(head);
    }
  
    // Driver Code
    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;
    }
      
// This code is contributed by Arnab Kundu

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to in-place product linked list
// nodes between 0s
   
// Linked List Node
class Node {
    int data;
    Node next;
   
    public Node(int data)
    {
        this.data = data;
        this.next = null;
    }
}
   
// Function to in-place product linked list
// nodes between 0s
public class inPlaceStoreLL {
   
    // Function to store numbers till 0
    static void inPlaceStore(Node head)
    {
        if (head.data == 0) {
            head = head.next;
        }
   
        // To store modified list
        Node res = head;
   
        // Traverse linked list and keep
        // adding nodes between 0s.
        Node temp = head;
        int prod = 1;
        while (temp != null) {
   
            // loop to product the data of nodes till
            // it encounters 0
            if (temp.data != 0) {
                prod *= temp.data;
                temp = temp.next;
            }
   
            // If we encounters 0, we need
            // to update next pointers
            else {
                  
                res.data = prod;
                res.next = temp.next;
                temp = temp.next;
                res = res.next;
                prod = 1;
            }
        }
          
        // For the last segment
        res.data = prod;
        res.next = temp;
                  
        printList(head);
    }
   
    // Function to traverse and print Linked List
    static void printList(Node head)
    {
        while (head.next != null) {
            System.out.print(head.data + "-> ");
            head = head.next;
        }
        System.out.println(head.data);
    }
   
    // Driver Code
    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);
    }
}

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to in-place product linked list 
// nodes between 0s 
using System;
  
// Linked List Node 
public class Node 
    public int data; 
    public Node next; 
  
    public Node(int data) 
    
        this.data = data; 
        this.next = null
    
  
// Function to in-place product linked list 
// nodes between 0s 
public class inPlaceStoreLL 
  
    // Function to store numbers till 0 
    static void inPlaceStore(Node head) 
    
        if (head.data == 0) 
        
            head = head.next; 
        
  
        // To store modified list 
        Node res = head; 
  
        // Traverse linked list and keep 
        // adding nodes between 0s. 
        Node temp = head; 
        int prod = 1; 
        while (temp != null)
        
  
            // loop to product the data of nodes till 
            // it encounters 0 
            if (temp.data != 0) 
            
                prod *= temp.data; 
                temp = temp.next; 
            
  
            // If we encounters 0, we need 
            // to update next pointers 
            else 
            
                  
                res.data = prod; 
                res.next = temp.next; 
                temp = temp.next; 
                res = res.next; 
                prod = 1; 
            
        
          
        // For the last segment 
        res.data = prod; 
        res.next = temp; 
                  
        printList(head); 
    
  
    // Function to traverse and print Linked List 
    static void printList(Node head) 
    
        while (head.next != null
        
            Console.Write(head.data + "-> "); 
            head = head.next; 
        
        Console.WriteLine(head.data); 
    
  
    // Driver Code 
    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); 
    
  
// This code has been contributed by 29AjayKumar

chevron_right


Output:

6-> 20-> 42


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : 29AjayKumar, andrew1234