Question: A Node is defined as follows: class Node { Node next; int value; } A linked list class MyLinkedList contains this Node declaration. It uses
A Node is defined as follows:
class Node
{
Node next;
int value; }
A linked list class MyLinkedList contains this Node declaration. It uses these Nodes (and no other data structures) to form and manipulate possibly empty singly linked lists. Give the code of an efficient member function for MyLinkedList called void combine() that takes no arguments and performs the following two operations during a single iterative scan of the linked list:
1) two or more neighboring Nodes with positive value elements are merged into one Node whose value becomes the sum of the values merged, and
2) two or more neighboring Nodes with negative value elements are merged into one Node whose value becomes the sum of the values merged. All other sequences of nodes are not changed.
For example, head 3 -5 -5 -2 6 -2 8 4 -2 10 becomes head 3 -12 6 -2 12 -2 10
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
