Sarthak Mehta
BAN USERFor simplification I haven't used concept of Encapsulation / data hiding.
public class Test {
public static void main(String[] args) {
LinkedList list = new LinkedList();
list.insert(4);
list.insert(6);
list.insert(-10);
list.insert(8);
list.insert(9);
list.insert(10);
list.insert(-19);
list.insert(10);
list.insert(-18);
list.insert(20);
list.insert(25);
list.removeZeroSum();
list.show();
}
}
public class LinkedList {
Node head;
public void insert(int data) {
if(head == null) {
Node node = new Node();
node.data = data;
node.next = null;
head = node;
} else {
Node search = head;
while(search.next != null) {
search = search.next;
}
Node node = new Node();
node.data = data;
node.next = null;
search.next = node;
}
}
public void removeZeroSum() {
int sum = 0;
Node subStart =head, subEnd = head;
while(subEnd != null) {
sum = sum + subEnd.data;
if(sum == 0) {
if(subStart == head) {
head = subEnd.next;
} else {
}
removeZeroSum();
break;
}
subEnd = subEnd.next;
}
Node join = subStart;
subEnd = subStart.next;
subStart = subStart.next;
sum = 0 ;
while(subEnd != null) {
sum = sum + subEnd.data;
if(sum == 0) {
if(join.next == null) {
break;
} else {
join.next = subEnd.next;
}
removeZeroSum();
break;
}
subEnd = subEnd.next;
}
}
}
Repbarrietrosado, abc at ABC TECH SUPPORT
Hi, I am Barrie, from Ualapue USA. I believe that every challenge has a solution - and I take great satisfaction ...
RepGinaSanchez, Computer Scientist at Autoportal.com
Ginna from New York.Successfully managing a high volume of work assignments without compromising quality to exceed client expectations.Apart ...
some problem in my code ... ignore
- Sarthak Mehta August 29, 2020