Hello,
I want to write a function which takes two linked list and check smaller
The function CheckSmaller should return 1 if num1 points to a linked list which represents a smaller number than the number pointed to by num2 linked list. Otherwise, it returns -1. If both linked list point to exactly the same number, CheckSmaller returns a 0.
#include<iostream>
using namespace std;
class Node
{
public:
int value;
Node * next;
Node();
Node(int);
};
Node::Node():value(0),next(NULL){}
Node::Node(int v):value(v),next(NULL){}
int checkSmallersmaller(Node *num1, Node *num2)
int sum (Node *num, int s)
{
if (num==0)
{
return s;
}
return sum (num->next, s*10+num->data);
}
int checkSmallersmaller(Node *num1, Node *num2)
{
int s1 = sum (num1, 0);
int s2 = sum (num2, 0);
if (s1< s2)
{
return 1;
}
else if (s1==s2)
{
return 0;
}
return -1;
}