Wednesday, July 3, 2024
HomeData ModellingData Structure & AlgorithmLinked List Sum of Nodes Between 0s

Linked List Sum of Nodes Between 0s

Given a linked list which contains a series of numbers separated by “0”. Add them and store them 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->9->5

Input  : 1->2->3->4
Output : 1->2->3->4
  1. Start iterating over nodes of the linked list.
  2. Iterate while temp.data !=0, and add these data into a variable ‘sum’.
  3. When you encounter 0 as the node’s data, change pointers of previous nodes.

Implementation:

C++




// C++ program to in-place add linked list
// nodes between 0s.
#include <bits/stdc++.h>
using namespace std;
#define NODE struct node
 
// Structure of a node
NODE
{
    int data;
    struct node *next;
};
 
// Function to create a node
NODE *getNode(int val)
{
    NODE *temp;
    temp = (NODE*)malloc(sizeof(NODE));
    temp->data = val;
    temp->next = NULL;
    return temp;
}
 
// Function to traverse and print Linked List
void printList(NODE *head)
{
    while(head->next)
    {
        cout << head->data << "-> ";
        head = head->next;
    }
    cout << "->" << head->data ;
}
 
void inPlaceStore(NODE *head)
{
     
    // Function to store numbers till 0
    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 sum = 0;
     
    while(temp)
    {
         
        // loop to sum the data of nodes till
        // it encounters 0
        if(temp->data != 0)
        {
            sum += temp->data;
            temp = temp->next;
        }
         
        // If we encounters 0, we need
        // to update next pointers
        else
        {
            res->data = sum;
            res->next = temp->next;
            temp = temp->next;
            res = temp;
            sum = 0;
        }
    }
    printList(head);
}
 
// Driver Code
int main()
{
     
    NODE *head;
    head = getNode(3);
    head->next = getNode(2);
    head->next->next = getNode(0);
    head->next->next->next = getNode(4);
    head->next->next->next->next = getNode(5);
    head->next->next->next->next->next = getNode(0);
    head->next->next->next->next->next->next = getNode(6);
    head->next->next->next->next->next->next->next = getNode(7);
    inPlaceStore(head);
    return 0;
}
 
// This code is contributed by shivanisinghss2110


C




// C program to in-place add linked list
// nodes between 0s.
#include <stdio.h>
#include<stdlib.h>
#define NODE struct node
 
// Structure of a node
NODE
{
    int data;
    struct node *next;
};
 
// Function to create a node
NODE *getNode(int val)
{
    NODE *temp;
    temp = (NODE*)malloc(sizeof(NODE));
    temp->data = val;
    temp->next = NULL;
    return temp;
}
 
// Function to traverse and print Linked List
void printList(NODE *head)
{
    while(head->next)
    {
        printf("%d->",head->data);
        head = head->next;
    }
    printf("%d\n",head->data);
}
 
void inPlaceStore(NODE *head)
{
     
    // Function to store numbers till 0
    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 sum = 0;
     
    while(temp)
    {
         
        // loop to sum the data of nodes till
        // it encounters 0
        if(temp->data != 0)
        {
            sum+=temp->data;
            temp = temp->next;
        }
         
         
        // If we encounters 0, we need
        // to update next pointers
        else
        {
             
            res->data = sum;
            res->next = temp->next;
            temp = temp->next;
            res = temp;
            sum = 0;
        }
    }
    printList(head);
}
 
// Driver Code
int main()
{
     
    NODE *head;
    head = getNode(3);
    head->next = getNode(2);
    head->next->next = getNode(0);
    head->next->next->next = getNode(4);
    head->next->next->next->next = getNode(5);
    head->next->next->next->next->next = getNode(0);
    head->next->next->next->next->next->next = getNode(6);
    head->next->next->next->next->next->next->next = getNode(7);
    inPlaceStore(head);
    return 0;
}
 
// This code is contributed by
// Kaustav kumar Chanda


Java




// Java program to in-place add linked list
// nodes between 0s.
class Node {
    int data;
    Node next;
 
    public Node(int data)
    {
        this.data = data;
        this.next = null;
    }
}
 
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 sum = 0;
        while (temp != null) {
             
             
             
            // loop to sum the data of nodes till
            // it encounters 0
            if (temp.data != 0) {
                sum += temp.data;
                temp = temp.next;
            }
 
            // If we encounters 0, we need
            // to update next pointers
            else {
                res.data = sum;
                res.next = temp.next;
                temp = res.next;
                res = res.next;
                sum = 0;
            }
        }
        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);
         
    }
}


Python3




# Python3 program to in-place add linked list
# nodes between 0s.
  
# Structure of a node
class Node:
     
    def __init__(self, data):
         
        self.data = data
        self.next = None
 
# Function to create a node
def getNode(val):
     
    temp = Node(val)
    return temp
     
# Function to traverse and print Linked List
def printList(head):
 
    while (head.next):
        print(head.data, end = "-> ")
        head = head.next
     
    print("->" + str(head.data), end = '')
 
def inPlaceStore(head):
      
    # Function to store numbers till 0
    if (head.data == 0):
        head = head.next
     
    # To store modified list
    res = head
      
    # Traverse linked list and keep
    # adding nodes between 0s.
    temp = head
    sum = 0
      
    while (temp):
          
        # Loop to sum the data of nodes till
        # it encounters 0
        if (temp.data != 0):
            sum += temp.data
            temp = temp.next
         
        # If we encounters 0, we need
        # to update next pointers
        else:
            res.data = sum
            res.next = temp.next
            temp = temp.next
            res = temp
            sum = 0
         
    printList(head)
 
# Driver Code
if __name__=='__main__':
      
    head = getNode(3)
    head.next = getNode(2)
    head.next.next = getNode(0)
    head.next.next.next = getNode(4)
    head.next.next.next.next = getNode(5)
    head.next.next.next.next.next = getNode(0)
    head.next.next.next.next.next.next = getNode(6)
    head.next.next.next.next.next.next.next = getNode(7)
     
    inPlaceStore(head)
     
# This code is contributed by rutvik_56


C#




// C# program to in-place add linked list
// nodes between 0s.
using System;
 
public class Node
{
    public int data;
    public Node next;
 
    public Node(int data)
    {
        this.data = data;
        this.next = null;
    }
}
 
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 sum = 0;
        while (temp != null)
        {
             
            // loop to sum the data of nodes till
            // it encounters 0
            if (temp.data != 0)
            {
                sum += temp.data;
                temp = temp.next;
            }
 
            // If we encounters 0, we need
            // to update next pointers
            else
            {
                res.data = sum;
                res.next = temp.next;
                temp = res.next;
                res = res.next;
                sum = 0;
            }
        }
        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()
    {
        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 is contributed PrinciRaj1992 */


Javascript




<script>
 
// JavaScript program to in-place add linked list
// nodes between 0s.
 
class Node
{
    constructor(data)
    {
        this.data=data;
        this.next = null;
    }
}
 
// Function to store numbers till 0
function inPlaceStore(head)
{
    if(head.data == 0){
           head = head.next;
        }
         
        // To store modified list
        let res = head;
  
        // Traverse linked list and keep
        // adding nodes between 0s.
        let temp = head;
        let sum = 0;
        while (temp != null) {
              
              
              
            // loop to sum the data of nodes till
            // it encounters 0
            if (temp.data != 0) {
                sum += temp.data;
                temp = temp.next;
            }
  
            // If we encounters 0, we need
            // to update next pointers
            else {
                res.data = sum;
                res.next = temp.next;
                temp = res.next;
                res = res.next;
                sum = 0;
            }
        }
        printList(head);
}
 
// Function to traverse and print Linked List
function  printList(head)
{
    while (head.next != null) {
            document.write(head.data + "-> ");
            head = head.next;
        }
        document.write(head.data);
}
 
// Driver Code
let 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 is contributed by rag2127
 
</script>


Output

5-> 9-> 6-> ->7

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Calisto Chipfumbu
Calisto Chipfumbuhttp://cchipfumbu@gmail.com
I have 5 years' worth of experience in the IT industry, primarily focused on Linux and Database administration. In those years, apart from learning significant technical knowledge, I also became comfortable working in a professional team and adapting to my environment, as I switched through 3 roles in that time.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments