Question: Problem: Write a C++ function using Recursion : void removeTrailingZeros(NodePtr &head) Task: Remove all zeros at the end of a number represented by linked list.

Problem: Write a C++ function using Recursion: void removeTrailingZeros(NodePtr &head)

Task: Remove all zeros at the end of a number represented by linked list. For example, if a number is 900700100000, the function should change the number to 9007001. If a number is 0, the linked list should be empty. The parameter head is passed by reference.

Precondition: head points to the first node of the linked list, or points to nullptr if the linked list is empty

This is my solution

.h file

struct Node { int value; Node *next; }; typedef Node *NodePtr; void removeTrailingZeros(NodePtr &head);

.cpp file

void removeTrailingZeros(NodePtr &head) {

 if (head == nullptr) { return; } if (head->value == 0) { NodePtr cur = head; head = head->next; delete cur; cur = nullptr; normalizeBigNum(head); } else { normalizeBigNum(head->next); } }

My solution here removes all the zeros in the number. I cannot figure out how to remove just the zeros at the end of the number.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!