Transfer the sum of all contiguous elements to right side zeros in a linked list

Here given code implementation process.

``````// C Program
// Transfer the sum of all contiguous elements to right side zeros in a linked list
#include <stdio.h>
#include <stdlib.h> //for malloc function
{
int data;
};
struct SingleLL
{
};
// Returns the new linked list
{
// Create memory of head and tail Nodes
struct SingleLL *sll = (struct SingleLL *) malloc(sizeof(struct SingleLL));
if (sll == NULL)
{
printf("Memory overflow\n");
}
else
{
}
return sll;
}
// Returns a new Node of linked list
{
// Create dynamic node
if (node == NULL)
{
printf("Memory overflow\n");
}
else
{
//Set initial node value
node->data = data;
node->next = NULL;
}
return node;
}
void addNode(struct SingleLL *sll, int data)
{
{
}
else
{
//Find last node
while (temp->next != NULL)
{
temp = temp->next;
}
// Append the node at last position
temp->next = node;
}
}
void display(struct SingleLL *sll)
{
{
return;
}
while (temp != NULL)
{
{
printf(" →");
}
printf(" %d", temp->data);
// Visit to next node
temp = temp->next;
}
printf(" → NULL\n");
}
// Move the linked list to the right by the sum of the contiguous non-zero elements
void transform(struct SingleLL *sll)
{
int sum = 0;
while (temp != NULL)
{
if (temp->data != 0)
{
sum += temp->data;
temp = temp->next;
}
else
{
temp->data = sum;
{
// First resultant node get
auxiliary = temp;
temp = temp->next;
}
else
{
auxiliary->next = temp;
auxiliary = temp;
temp = temp->next;
}
// Avoid contiguous zero
while (temp != NULL && temp->data == 0)
{
temp = temp->next;
}
sum = 0;
}
}
{
// Linked list not converted because
// When no zero exists in this linked list
// Or all zero nodes exist in linked list
}
}
int main()
{
// Create a empty linked lists
//  2 → 5 → 0 → 8 → 0 → 4 → 9 → 7 → 0 → 1 NULL
printf(" Before Convert \n");
display(sll1);
transform(sll1);
printf(" After Convert \n");
display(sll1);
//  1 → 2 → 0 → 0 → 0 → 6 → 0 → NULL
printf(" Before Convert \n");
display(sll2);
transform(sll2);
printf(" After Convert \n");
display(sll2);
//  1 → 2 → 3 → NULL
printf(" Before Convert \n");
display(sll3);
transform(sll3);
printf(" After Convert \n");
display(sll3);
return 0;
}``````

Output

`````` Before Convert
2 → 5 → 0 → 8 → 0 → 4 → 9 → 7 → 0 → 1 → NULL
After Convert
7 → 8 → 20 → 1 → NULL
Before Convert
1 → 2 → 0 → 0 → 0 → 6 → 0 → NULL
After Convert
3 → 6 → NULL
Before Convert
1 → 2 → 3 → NULL
After Convert
1 → 2 → 3 → NULL``````
``````/*
Java Program for
Transfer the sum of all contiguous elements to right side zeros in a linked list
*/
{
public int data;
{
this.data = data;
this.next = null;
}
};
public class SingleLL
{
public SingleLL()
{
}
{
{
}
else
{
//Find last node
while (temp.next != null)
{
temp = temp.next;
}
// Append the node at last position
temp.next = node;
}
}
public void display()
{
{
return;
}
while (temp != null)
{
{
System.out.print(" →");
}
System.out.print(" " + temp.data);
// Visit to next node
temp = temp.next;
}
System.out.print(" → NULL\n");
}
// Move the linked list to the right by the sum of the contiguous non-zero elements
public void transform()
{
int sum = 0;
while (temp != null)
{
if (temp.data != 0)
{
sum += temp.data;
temp = temp.next;
}
else
{
temp.data = sum;
{
// First resultant node get
auxiliary = temp;
temp = temp.next;
}
else
{
auxiliary.next = temp;
auxiliary = temp;
temp = temp.next;
}
// Avoid contiguous zero
while (temp != null && temp.data == 0)
{
temp = temp.next;
}
sum = 0;
}
}
{
// Linked list not converted because
// When no zero exists in this linked list
// Or all zero nodes exist in linked list
}
}
public static void main(String[] args)
{
// Create a empty linked lists
SingleLL sll1 = new SingleLL();
SingleLL sll2 = new SingleLL();
SingleLL sll3 = new SingleLL();
//  2 → 5 → 0 → 8 → 0 → 4 → 9 → 7 → 0 → 1 NULL
System.out.print(" Before Convert \n");
sll1.display();
sll1.transform();
System.out.print(" After Convert \n");
sll1.display();
//  1 → 2 → 0 → 0 → 0 → 6 → 0 → NULL
System.out.print(" Before Convert \n");
sll2.display();
sll2.transform();
System.out.print(" After Convert \n");
sll2.display();
//  1 → 2 → 3 → NULL
System.out.print(" Before Convert \n");
sll3.display();
sll3.transform();
System.out.print(" After Convert \n");
sll3.display();
}
}``````

Output

`````` Before Convert
2 → 5 → 0 → 8 → 0 → 4 → 9 → 7 → 0 → 1 → NULL
After Convert
7 → 8 → 20 → 1 → NULL
Before Convert
1 → 2 → 0 → 0 → 0 → 6 → 0 → NULL
After Convert
3 → 6 → NULL
Before Convert
1 → 2 → 3 → NULL
After Convert
1 → 2 → 3 → NULL``````
``````// Include header file
#include <iostream>

using namespace std;
/*
C++ Program for
Transfer the sum of all contiguous elements to right side zeros in a linked list
*/
{
public: int data;
{
this->data = data;
this->next = NULL;
}
};;
class SingleLL
{
SingleLL()
{
}
{
{
}
else
{
//Find last node
while (temp->next != NULL)
{
temp = temp->next;
}
// Append the node at last position
temp->next = node;
}
}
void display()
{
{
cout << "\n Empty linked list\n";
return;
}
while (temp != NULL)
{
{
cout << " →";
}
cout << " " << temp->data;
// Visit to next node
temp = temp->next;
}
cout << " → NULL\n";
}
// Move the linked list to the right by the sum of the contiguous non-zero elements
void transform()
{
int sum = 0;
while (temp != NULL)
{
if (temp->data != 0)
{
sum += temp->data;
temp = temp->next;
}
else
{
temp->data = sum;
{
// First resultant node get
auxiliary = temp;
temp = temp->next;
}
else
{
auxiliary->next = temp;
auxiliary = temp;
temp = temp->next;
}
// Avoid contiguous zero
while (temp != NULL && temp->data == 0)
{
temp = temp->next;
}
sum = 0;
}
}
{
// Linked list not converted because
// When no zero exists in this linked list
// Or all zero nodes exist in linked list
}
}
};
int main()
{
// Create a empty linked lists
SingleLL sll1 = SingleLL();
SingleLL sll2 = SingleLL();
SingleLL sll3 = SingleLL();
//  2 → 5 → 0 → 8 → 0 → 4 → 9 → 7 → 0 → 1 NULL
cout << " Before Convert \n";
sll1.display();
sll1.transform();
cout << " After Convert \n";
sll1.display();
//  1 → 2 → 0 → 0 → 0 → 6 → 0 → NULL
cout << " Before Convert \n";
sll2.display();
sll2.transform();
cout << " After Convert \n";
sll2.display();
//  1 → 2 → 3 → NULL
cout << " Before Convert \n";
sll3.display();
sll3.transform();
cout << " After Convert \n";
sll3.display();
return 0;
}``````

Output

`````` Before Convert
2 → 5 → 0 → 8 → 0 → 4 → 9 → 7 → 0 → 1 → NULL
After Convert
7 → 8 → 20 → 1 → NULL
Before Convert
1 → 2 → 0 → 0 → 0 → 6 → 0 → NULL
After Convert
3 → 6 → NULL
Before Convert
1 → 2 → 3 → NULL
After Convert
1 → 2 → 3 → NULL``````
``````// Include namespace system
using System;
/*
C# Program for
Transfer the sum of all contiguous elements to right side zeros in a linked list
*/
{
public int data;
{
this.data = data;
this.next = null;
}
};
public class SingleLL
{
public SingleLL()
{
}
{
{
}
else
{
//Find last node
while (temp.next != null)
{
temp = temp.next;
}
// Append the node at last position
temp.next = node;
}
}
public void display()
{
{
return;
}
while (temp != null)
{
{
Console.Write(" →");
}
Console.Write(" " + temp.data);
// Visit to next node
temp = temp.next;
}
Console.Write(" → NULL\n");
}
// Move the linked list to the right by the sum of the contiguous non-zero elements
public void transform()
{
int sum = 0;
while (temp != null)
{
if (temp.data != 0)
{
sum += temp.data;
temp = temp.next;
}
else
{
temp.data = sum;
{
// First resultant node get
auxiliary = temp;
temp = temp.next;
}
else
{
auxiliary.next = temp;
auxiliary = temp;
temp = temp.next;
}
// Avoid contiguous zero
while (temp != null && temp.data == 0)
{
temp = temp.next;
}
sum = 0;
}
}
{
// Linked list not converted because
// When no zero exists in this linked list
// Or all zero nodes exist in linked list
}
}
public static void Main(String[] args)
{
// Create a empty linked lists
SingleLL sll1 = new SingleLL();
SingleLL sll2 = new SingleLL();
SingleLL sll3 = new SingleLL();
//  2 → 5 → 0 → 8 → 0 → 4 → 9 → 7 → 0 → 1 NULL
Console.Write(" Before Convert \n");
sll1.display();
sll1.transform();
Console.Write(" After Convert \n");
sll1.display();
//  1 → 2 → 0 → 0 → 0 → 6 → 0 → NULL
Console.Write(" Before Convert \n");
sll2.display();
sll2.transform();
Console.Write(" After Convert \n");
sll2.display();
//  1 → 2 → 3 → NULL
Console.Write(" Before Convert \n");
sll3.display();
sll3.transform();
Console.Write(" After Convert \n");
sll3.display();
}
}``````

Output

`````` Before Convert
2 → 5 → 0 → 8 → 0 → 4 → 9 → 7 → 0 → 1 → NULL
After Convert
7 → 8 → 20 → 1 → NULL
Before Convert
1 → 2 → 0 → 0 → 0 → 6 → 0 → NULL
After Convert
3 → 6 → NULL
Before Convert
1 → 2 → 3 → NULL
After Convert
1 → 2 → 3 → NULL``````
``````<?php
/*
Php Program for
Transfer the sum of all contiguous elements to right side zeros in a linked list
*/
{
public \$data;
public \$next;

function __construct(\$data)
{
\$this->data = \$data;
\$this->next = null;
}
};
class SingleLL
{

function __construct()
{
}
{
{
}
else
{
//Find last node
while (\$temp->next != null)
{
\$temp = \$temp->next;
}
// Append the node at last position
\$temp->next = \$node;
}
}
public	function display()
{
{
return;
}
while (\$temp != null)
{
{
echo " →";
}
echo " ". \$temp->data;
// Visit to next node
\$temp = \$temp->next;
}
echo " → NULL\n";
}
// Move the linked list to the right by the sum of the contiguous non-zero elements
public	function transform()
{
\$sum = 0;
while (\$temp != null)
{
if (\$temp->data != 0)
{
\$sum += \$temp->data;
\$temp = \$temp->next;
}
else
{
\$temp->data = \$sum;
{
// First resultant node get
\$auxiliary = \$temp;
\$temp = \$temp->next;
}
else
{
\$auxiliary->next = \$temp;
\$auxiliary = \$temp;
\$temp = \$temp->next;
}
// Avoid contiguous zero
while (\$temp != null && \$temp->data == 0)
{
\$temp = \$temp->next;
}
\$sum = 0;
}
}
{
// Linked list not converted because
// When no zero exists in this linked list
// Or all zero nodes exist in linked list
}
}
}

function main()
{
// Create a empty linked lists
\$sll1 = new SingleLL();
\$sll2 = new SingleLL();
\$sll3 = new SingleLL();
//  2 → 5 → 0 → 8 → 0 → 4 → 9 → 7 → 0 → 1 NULL
echo " Before Convert \n";
\$sll1->display();
\$sll1->transform();
echo " After Convert \n";
\$sll1->display();
//  1 → 2 → 0 → 0 → 0 → 6 → 0 → NULL
echo " Before Convert \n";
\$sll2->display();
\$sll2->transform();
echo " After Convert \n";
\$sll2->display();
//  1 → 2 → 3 → NULL
echo " Before Convert \n";
\$sll3->display();
\$sll3->transform();
echo " After Convert \n";
\$sll3->display();
}
main();``````

Output

`````` Before Convert
2 → 5 → 0 → 8 → 0 → 4 → 9 → 7 → 0 → 1 → NULL
After Convert
7 → 8 → 20 → 1 → NULL
Before Convert
1 → 2 → 0 → 0 → 0 → 6 → 0 → NULL
After Convert
3 → 6 → NULL
Before Convert
1 → 2 → 3 → NULL
After Convert
1 → 2 → 3 → NULL``````
``````/*
Node Js Program for
Transfer the sum of all contiguous elements to right side zeros in a linked list
*/
{
constructor(data)
{
this.data = data;
this.next = null;
}
};
class SingleLL
{
constructor()
{
}
{
{
}
else
{
//Find last node
while (temp.next != null)
{
temp = temp.next;
}
// Append the node at last position
temp.next = node;
}
}
display()
{
{
return;
}
while (temp != null)
{
{
process.stdout.write(" →");
}
process.stdout.write(" " + temp.data);
// Visit to next node
temp = temp.next;
}
process.stdout.write(" → NULL\n");
}
// Move the linked list to the right by the sum of the contiguous non-zero elements
transform()
{
var sum = 0;
while (temp != null)
{
if (temp.data != 0)
{
sum += temp.data;
temp = temp.next;
}
else
{
temp.data = sum;
{
// First resultant node get
auxiliary = temp;
temp = temp.next;
}
else
{
auxiliary.next = temp;
auxiliary = temp;
temp = temp.next;
}
// Avoid contiguous zero
while (temp != null && temp.data == 0)
{
temp = temp.next;
}
sum = 0;
}
}
{
// Linked list not converted because
// When no zero exists in this linked list
// Or all zero nodes exist in linked list
}
}
}

function main()
{
// Create a empty linked lists
var sll1 = new SingleLL();
var sll2 = new SingleLL();
var sll3 = new SingleLL();
//  2 → 5 → 0 → 8 → 0 → 4 → 9 → 7 → 0 → 1 NULL
process.stdout.write(" Before Convert \n");
sll1.display();
sll1.transform();
process.stdout.write(" After Convert \n");
sll1.display();
//  1 → 2 → 0 → 0 → 0 → 6 → 0 → NULL
process.stdout.write(" Before Convert \n");
sll2.display();
sll2.transform();
process.stdout.write(" After Convert \n");
sll2.display();
//  1 → 2 → 3 → NULL
process.stdout.write(" Before Convert \n");
sll3.display();
sll3.transform();
process.stdout.write(" After Convert \n");
sll3.display();
}
main();``````

Output

`````` Before Convert
2 → 5 → 0 → 8 → 0 → 4 → 9 → 7 → 0 → 1 → NULL
After Convert
7 → 8 → 20 → 1 → NULL
Before Convert
1 → 2 → 0 → 0 → 0 → 6 → 0 → NULL
After Convert
3 → 6 → NULL
Before Convert
1 → 2 → 3 → NULL
After Convert
1 → 2 → 3 → NULL``````
``````#   Python 3 Program for
#   Transfer the sum of all contiguous elements to right side zeros in a linked list

def __init__(self, data) :
self.data = data
self.next = None

class SingleLL :

def __init__(self) :

else :
# Find last node
while (temp.next != None) :
temp = temp.next

#  Append the node at last position
temp.next = node

def display(self) :
return

while (temp != None) :
print(" →", end = "")

print(" ", temp.data, end = "")
#  Visit to next node
temp = temp.next

print(" → NULL")

#  Move the linked list to the right by the sum of the contiguous non-zero elements
def transform(self) :
sum = 0
while (temp != None) :
if (temp.data != 0) :
sum += temp.data
temp = temp.next
else :
temp.data = sum
#  First resultant node get
auxiliary = temp
temp = temp.next
else :
auxiliary.next = temp
auxiliary = temp
temp = temp.next

#  Avoid contiguous zero
while (temp != None and temp.data == 0) :
temp = temp.next

sum = 0

#  Linked list not converted because
#  When no zero exists in this linked list
#  Or all zero nodes exist in linked list

def main() :
#  Create a empty linked lists
sll1 = SingleLL()
sll2 = SingleLL()
sll3 = SingleLL()
#   2 → 5 → 0 → 8 → 0 → 4 → 9 → 7 → 0 → 1 NULL
print(" Before Convert ")
sll1.display()
sll1.transform()
print(" After Convert ")
sll1.display()
#   1 → 2 → 0 → 0 → 0 → 6 → 0 → NULL
print(" Before Convert ")
sll2.display()
sll2.transform()
print(" After Convert ")
sll2.display()
#   1 → 2 → 3 → NULL
print(" Before Convert ")
sll3.display()
sll3.transform()
print(" After Convert ")
sll3.display()

if __name__ == "__main__": main()``````

Output

`````` Before Convert
2 →  5 →  0 →  8 →  0 →  4 →  9 →  7 →  0 →  1 → NULL
After Convert
7 →  8 →  20 →  1 → NULL
Before Convert
1 →  2 →  0 →  0 →  0 →  6 →  0 → NULL
After Convert
3 →  6 → NULL
Before Convert
1 →  2 →  3 → NULL
After Convert
1 →  2 →  3 → NULL``````
``````#   Ruby Program for
#   Transfer the sum of all contiguous elements to right side zeros in a linked list

attr_accessor :data, :next

def initialize(data)
self.data = data
self.next = nil
end

end

class SingleLL
# Define the accessor and reader of class SingleLL

def initialize()
end

else
# Find last node
while (temp.next != nil)
temp = temp.next
end

#  Append the node at last position
temp.next = node
end

end

def display()
return
end

while (temp != nil)
print(" →")
end

print(" ", temp.data)
#  Visit to next node
temp = temp.next
end

print(" → NULL\n")
end

#  Move the linked list to the right by the sum of the contiguous non-zero elements
def transform()
sum = 0
while (temp != nil)
if (temp.data != 0)
sum += temp.data
temp = temp.next
else
temp.data = sum
#  First resultant node get
auxiliary = temp
temp = temp.next
else
auxiliary.next = temp
auxiliary = temp
temp = temp.next
end

#  Avoid contiguous zero
while (temp != nil && temp.data == 0)
temp = temp.next
end

sum = 0
end

end

#  Linked list not converted because
#  When no zero exists in this linked list
#  Or all zero nodes exist in linked list
end

end

end

def main()
#  Create a empty linked lists
sll1 = SingleLL.new()
sll2 = SingleLL.new()
sll3 = SingleLL.new()
#   2 → 5 → 0 → 8 → 0 → 4 → 9 → 7 → 0 → 1 NULL
print(" Before Convert \n")
sll1.display()
sll1.transform()
print(" After Convert \n")
sll1.display()
#   1 → 2 → 0 → 0 → 0 → 6 → 0 → NULL
print(" Before Convert \n")
sll2.display()
sll2.transform()
print(" After Convert \n")
sll2.display()
#   1 → 2 → 3 → NULL
print(" Before Convert \n")
sll3.display()
sll3.transform()
print(" After Convert \n")
sll3.display()
end

main()``````

Output

`````` Before Convert
2 → 5 → 0 → 8 → 0 → 4 → 9 → 7 → 0 → 1 → NULL
After Convert
7 → 8 → 20 → 1 → NULL
Before Convert
1 → 2 → 0 → 0 → 0 → 6 → 0 → NULL
After Convert
3 → 6 → NULL
Before Convert
1 → 2 → 3 → NULL
After Convert
1 → 2 → 3 → NULL
``````
``````/*
Scala Program for
Transfer the sum of all contiguous elements to right side zeros in a linked list
*/
{
def this(data: Int)
{
this(data, null);
}
};
{
def this()
{
this(null);
}
def addNode(data: Int): Unit = {
{
}
else
{
//Find last node
while (temp.next != null)
{
temp = temp.next;
}
// Append the node at last position
temp.next = node;
}
}
def display(): Unit = {
{
return;
}
while (temp != null)
{
{
print(" →");
}
print(" " + temp.data);
// Visit to next node
temp = temp.next;
}
print(" → NULL\n");
}
// Move the linked list to the right by the sum of the contiguous non-zero elements
def transform(): Unit = {
var sum: Int = 0;
while (temp != null)
{
if (temp.data != 0)
{
sum += temp.data;
temp = temp.next;
}
else
{
temp.data = sum;
{
// First resultant node get
auxiliary = temp;
temp = temp.next;
}
else
{
auxiliary.next = temp;
auxiliary = temp;
temp = temp.next;
}
// Avoid contiguous zero
while (temp != null && temp.data == 0)
{
temp = temp.next;
}
sum = 0;
}
}
{
// Linked list not converted because
// When no zero exists in this linked list
// Or all zero nodes exist in linked list
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
// Create a empty linked lists
var sll1: SingleLL = new SingleLL();
var sll2: SingleLL = new SingleLL();
var sll3: SingleLL = new SingleLL();
//  2 → 5 → 0 → 8 → 0 → 4 → 9 → 7 → 0 → 1 NULL
print(" Before Convert \n");
sll1.display();
sll1.transform();
print(" After Convert \n");
sll1.display();
//  1 → 2 → 0 → 0 → 0 → 6 → 0 → NULL
print(" Before Convert \n");
sll2.display();
sll2.transform();
print(" After Convert \n");
sll2.display();
//  1 → 2 → 3 → NULL
print(" Before Convert \n");
sll3.display();
sll3.transform();
print(" After Convert \n");
sll3.display();
}
}``````

Output

`````` Before Convert
2 → 5 → 0 → 8 → 0 → 4 → 9 → 7 → 0 → 1 → NULL
After Convert
7 → 8 → 20 → 1 → NULL
Before Convert
1 → 2 → 0 → 0 → 0 → 6 → 0 → NULL
After Convert
3 → 6 → NULL
Before Convert
1 → 2 → 3 → NULL
After Convert
1 → 2 → 3 → NULL``````
``````/*
Swift 4 Program for
Transfer the sum of all contiguous elements to right side zeros in a linked list
*/
{
var data: Int;
init(_ data: Int)
{
self.data = data;
self.next = nil;
}
};
class SingleLL
{
init()
{
}
{
{
}
else
{
//Find last node
while (temp!.next  != nil)
{
temp = temp!.next;
}
// Append the node at last position
temp!.next = node;
}
}
func display()
{
{
return;
}
while (temp  != nil)
{
{
print(" →", terminator: "");
}
print(" ", temp!.data, terminator: "");
// Visit to next node
temp = temp!.next;
}
print(" → NULL");
}
// Move the linked list to the right by the sum of the contiguous non-zero elements
func transform()
{
var sum: Int = 0;
while (temp  != nil)
{
if (temp!.data  != 0)
{
sum += temp!.data;
temp = temp!.next;
}
else
{
temp!.data = sum;
{
// First resultant node get
auxiliary = temp;
temp = temp!.next;
}
else
{
auxiliary!.next = temp;
auxiliary = temp;
temp = temp!.next;
}
// Avoid contiguous zero
while (temp  != nil && temp!.data == 0)
{
temp = temp!.next;
}
sum = 0;
}
}
{
// Linked list not converted because
// When no zero exists in this linked list
// Or all zero nodes exist in linked list
}
}
}
func main()
{
// Create a empty linked lists
let sll1: SingleLL = SingleLL();
let sll2: SingleLL = SingleLL();
let sll3: SingleLL = SingleLL();
//  2 → 5 → 0 → 8 → 0 → 4 → 9 → 7 → 0 → 1 NULL
print(" Before Convert ");
sll1.display();
sll1.transform();
print(" After Convert ");
sll1.display();
//  1 → 2 → 0 → 0 → 0 → 6 → 0 → NULL
print(" Before Convert ");
sll2.display();
sll2.transform();
print(" After Convert ");
sll2.display();
//  1 → 2 → 3 → NULL
print(" Before Convert ");
sll3.display();
sll3.transform();
print(" After Convert ");
sll3.display();
}
main();``````

Output

`````` Before Convert
2 →  5 →  0 →  8 →  0 →  4 →  9 →  7 →  0 →  1 → NULL
After Convert
7 →  8 →  20 →  1 → NULL
Before Convert
1 →  2 →  0 →  0 →  0 →  6 →  0 → NULL
After Convert
3 →  6 → NULL
Before Convert
1 →  2 →  3 → NULL
After Convert
1 →  2 →  3 → NULL``````
``````/*
Kotlin Program for
Transfer the sum of all contiguous elements to right side zeros in a linked list
*/
{
var data: Int;
constructor(data: Int)
{
this.data = data;
this.next = null;
}
};
class SingleLL
{
constructor()
{
}
{
{
}
else
{
//Find last node
while (temp?.next != null)
{
temp = temp.next;
}
// Append the node at last position
temp?.next = node;
}
}
fun display(): Unit
{
{
return;
}
while (temp != null)
{
{
print(" →");
}
print(" " + temp.data);
// Visit to next node
temp = temp.next;
}
print(" → NULL\n");
}
// Move the linked list to the right by the sum of the contiguous non-zero elements
fun transform(): Unit
{
var sum: Int = 0;
while (temp != null)
{
if (temp.data != 0)
{
sum += temp.data;
temp = temp.next;
}
else
{
temp.data = sum;
{
// First resultant node get
auxiliary = temp;
temp = temp.next;
}
else
{
auxiliary?.next = temp;
auxiliary = temp;
temp = temp.next;
}
// Avoid contiguous zero
while (temp != null && temp.data == 0)
{
temp = temp.next;
}
sum = 0;
}
}
{
// Linked list not converted because
// When no zero exists in this linked list
// Or all zero nodes exist in linked list
}
}
}
fun main(args: Array < String > ): Unit
{
// Create a empty linked lists
var sll1: SingleLL = SingleLL();
var sll2: SingleLL = SingleLL();
var sll3: SingleLL = SingleLL();
//  2 → 5 → 0 → 8 → 0 → 4 → 9 → 7 → 0 → 1 NULL
print(" Before Convert \n");
sll1.display();
sll1.transform();
print(" After Convert \n");
sll1.display();
//  1 → 2 → 0 → 0 → 0 → 6 → 0 → NULL
print(" Before Convert \n");
sll2.display();
sll2.transform();
print(" After Convert \n");
sll2.display();
//  1 → 2 → 3 → NULL
print(" Before Convert \n");
sll3.display();
sll3.transform();
print(" After Convert \n");
sll3.display();
}``````

Output

`````` Before Convert
2 → 5 → 0 → 8 → 0 → 4 → 9 → 7 → 0 → 1 → NULL
After Convert
7 → 8 → 20 → 1 → NULL
Before Convert
1 → 2 → 0 → 0 → 0 → 6 → 0 → NULL
After Convert
3 → 6 → NULL
Before Convert
1 → 2 → 3 → NULL
After Convert
1 → 2 → 3 → NULL``````

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.