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
- Start iterating over nodes of the linked list.
- Iterate while temp.data !=0, and add these data into a variable ‘sum’.
- When you encounter 0 as the node’s data, change pointers of previous nodes.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
#define NODE struct node
NODE
{
int data;
struct node *next;
};
NODE *getNode( int val)
{
NODE *temp;
temp = (NODE*) malloc ( sizeof (NODE));
temp->data = val;
temp->next = NULL;
return temp;
}
void printList(NODE *head)
{
while (head->next)
{
cout << head->data << "-> " ;
head = head->next;
}
cout << "->" << head->data ;
}
void inPlaceStore(NODE *head)
{
if (head->data == 0)
{
head = head->next;
}
NODE *res = head;
NODE *temp = head;
int sum = 0;
while (temp)
{
if (temp->data != 0)
{
sum += temp->data;
temp = temp->next;
}
else
{
res->data = sum;
res->next = temp->next;
temp = temp->next;
res = temp;
sum = 0;
}
}
printList(head);
}
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;
}
|
C
#include <stdio.h>
#include<stdlib.h>
#define NODE struct node
NODE
{
int data;
struct node *next;
};
NODE *getNode( int val)
{
NODE *temp;
temp = (NODE*) malloc ( sizeof (NODE));
temp->data = val;
temp->next = NULL;
return temp;
}
void printList(NODE *head)
{
while (head->next)
{
printf ( "%d->" ,head->data);
head = head->next;
}
printf ( "%d\n" ,head->data);
}
void inPlaceStore(NODE *head)
{
if (head->data == 0)
{
head = head->next;
}
NODE *res = head;
NODE *temp = head;
int sum = 0;
while (temp)
{
if (temp->data != 0)
{
sum+=temp->data;
temp = temp->next;
}
else
{
res->data = sum;
res->next = temp->next;
temp = temp->next;
res = temp;
sum = 0;
}
}
printList(head);
}
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;
}
|
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 sum = 0 ;
while (temp != null ) {
if (temp.data != 0 ) {
sum += temp.data;
temp = temp.next;
}
else {
res.data = sum;
res.next = temp.next;
temp = res.next;
res = res.next;
sum = 0 ;
}
}
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
class Node:
def __init__( self , data):
self .data = data
self . next = None
def getNode(val):
temp = Node(val)
return temp
def printList(head):
while (head. next ):
print (head.data, end = "-> " )
head = head. next
print ( "->" + str (head.data), end = '')
def inPlaceStore(head):
if (head.data = = 0 ):
head = head. next
res = head
temp = head
sum = 0
while (temp):
if (temp.data ! = 0 ):
sum + = temp.data
temp = temp. next
else :
res.data = sum
res. next = temp. next
temp = temp. next
res = temp
sum = 0
printList(head)
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)
|
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 sum = 0;
while (temp != null )
{
if (temp.data != 0)
{
sum += temp.data;
temp = temp.next;
}
else
{
res.data = sum;
res.next = temp.next;
temp = res.next;
res = res.next;
sum = 0;
}
}
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()
{
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;
}
let res = head;
let temp = head;
let sum = 0;
while (temp != null ) {
if (temp.data != 0) {
sum += temp.data;
temp = temp.next;
}
else {
res.data = sum;
res.next = temp.next;
temp = res.next;
res = res.next;
sum = 0;
}
}
printList(head);
}
function printList(head)
{
while (head.next != null ) {
document.write(head.data + "-> " );
head = head.next;
}
document.write(head.data);
}
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);
</script>
|
Last Updated :
27 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...