Ejemplo n.º 1
0
int addWithCarry(struct Node* current)
{
	if(current==NULL)
		return 1;
	int sum=current->data+addWithCarry(current->next);
	current->data=sum%10;
	return sum/10;
}
Ejemplo n.º 2
0
 ListNode* addWithCarry(ListNode* l1, ListNode* l2, int carry) {
     if (l1 == NULL && l2 == NULL && carry == 0)   return NULL;
     int value = carry;
     if (l1)    value += l1->val;
     if (l2)    value += l2->val;
     ListNode* result = new ListNode(value%10);
     result->next = addWithCarry(l1 == NULL ? NULL : l1->next,
                                 l2 == NULL ? NULL : l2->next,
                                 value > 9 ? 1 : 0);
     return result;
 }
Ejemplo n.º 3
0
struct Node* add1(struct Node* head)
{
	int result=addWithCarry(head);
	if(result)
	{
		struct Node *newnode=(struct Node*)malloc(sizeof(struct Node));
		newnode->data=result;
		newnode->next=head;
		head=newnode;
	}
	return head;
}
Ejemplo n.º 4
0
 ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
     // Start typing your C/C++ solution below
     // DO NOT write int main() function
     return addWithCarry(l1, l2, 0);
 }