Problem Description
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Link to LeetCode. Only code submitted to LeetCode is in AddTwoNumbersHelper
and AddTwoNumbers
.
I've written this so that anyone can compile and run this program on their machines with:
g++ -std=c++11 -Wall -Wextra -Werror Main.cpp -o main ; ./main
I'm looking for feedback on this LeetCode question after modifying the code from feedback in my Original Question. Here is what has changed:
The solution now uses recursion
The main logic is not spaghetti code anymore
Dealt with dangling pointers
What I'd like some feedback on
I'm still a bit unclear on the best practice for how to handle the namespace
and using
s, as well as how to find the time and space complexity for this.
#include <cstddef>
#include <iostream>
#include <ostream>
#include <stddef.h>
#include <stdlib.h>
using std::cout;
using std::endl;
using std::ostream;
using std::string;
struct Node
{
int val;
Node *next;
Node(int val) : val(val), next(NULL) {}
void PrintAllNodes()
{
Node *current = new Node(0);
current = this;
std::string nodeString = "LinkedList: ";
int val = 0;
while(current != NULL)
{
val = current->val;
nodeString = nodeString + " -> ( " + std::to_string(val) + " ) ";
current = current->next;
}
std::cout << nodeString << '\n';
}
void Append(int i)
{
if (this->next == NULL)
{
Node *n = new Node(i);
this->next = n;
}
else { this->next->Append(i); }
}
};
class Solution
{
public:
Node *AddTwoNumbers(Node *l1, Node *l2);
private:
Node *AddTwoNumbersHelper(Node *l1, Node *l2, int sum, int carry, Node *current, Node *head);
};
Node *Solution::AddTwoNumbers(Node *l1, Node *l2)
{
Node *head = new Node(0);
Node *current = head;
int sum = 0;
int carry = 0;
return AddTwoNumbersHelper(l1, l2, sum, carry, current, head);
}
Node *Solution::AddTwoNumbersHelper(Node *l1, Node *l2, int sum, int carry, Node *current, Node *head)
{
if (l1 == NULL && l2 == NULL)
{
head = head->next;
return head;
}
sum = 0;
if (l1 == NULL) { sum = l2->val + carry; }
else if (l2 == NULL) { sum = l1->val + carry; }
else if (l1 != NULL && l2 != NULL) { sum = l1->val + l2->val + carry; }
if (sum >= 10)
{
carry = sum / 10;
sum -= 10;
}
else { carry = 0; }
Node *next = new Node(sum);
current->next = next;
if (l1 == NULL) { return AddTwoNumbersHelper(l1, l2->next, sum, carry, current->next, head); }
else if (l2 == NULL) { return AddTwoNumbersHelper(l1->next, l2, sum, carry, current->next, head); }
else if (l1 != NULL && l2 != NULL) { return AddTwoNumbersHelper(l1->next, l2->next, sum, carry, current->next, head); }
return head;
}
/**
* Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
* Output: 7 -> 0 -> 8
* Explanation: 342 + 465 = 807.
*/
void ProveBasicCase()
{
cout << "\n\nBasic case\n";
Solution s;
Node *l1 = new Node(2);
l1->Append(4);
l1->Append(3);
Node *l2 = new Node(5);
l2->Append(6);
l2->Append(4);
Node *n = new Node(0);
n = s.AddTwoNumbers(l1, l2);
n->PrintAllNodes();
}
/**
* Input: (2 -> 4 -> 3) + (5 -> 6)
* Output: 7 -> 0 -> 4
* Explanation: 342 + 65 = 407.
*/
void ProveUnEqualListSize()
{
cout << "\n\nUneven List sizes\n";
Solution s;
Node *l1 = new Node(2);
l1->Append(4);
l1->Append(3);
Node *l2 = new Node(5);
l2->Append(6);
Node *n = new Node(0);
n = s.AddTwoNumbers(l1, l2);
n->PrintAllNodes();
}
/**
* Input: (9) + (1 -> 9 -> 9 -> 9 -> 8 -> 9 -> 9)
* Output: 0 -> 0 -> 0 -> 0 -> 9 -> 9 -> 9
* Explanation: 9 + 9989991 = 9990000
*/
void ProveDoubleCarry()
{
cout << "\n\nDouble Carry\n";
Solution s;
Node *l1 = new Node(9);
Node *l2 = new Node(1);
l2->Append(9);
l2->Append(9);
l2->Append(9);
l2->Append(8);
l2->Append(9);
l2->Append(9);
Node *n = new Node(0);
n = s.AddTwoNumbers(l1, l2);
n->PrintAllNodes();
}
int main()
{
cout << "mr.robot prgm running...\n";
ProveBasicCase();
ProveUnEqualListSize();
ProveDoubleCarry();
return 0;
}
g++
into./main
? Did you mean to use a semicolon, or a logical operator of some sort? \$\endgroup\$g++
and tries to put it into your program. You probably meant&&
or;
instead of|
. See: stackoverflow.com/q/9834086/6789498 \$\endgroup\$