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.
Example:
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.
code:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution
{
public:
ListNode* addTwoNumbers(ListNode* list_a, ListNode* list_b)
{
int list_a_len = 0;
int list_b_len = 0;
ListNode * list_a_ptr = list_a;
ListNode * list_b_ptr = list_b;
while(list_a_ptr->next != NULL)
{
list_a_len ++;
list_a_ptr = list_a_ptr->next;
}
while(list_b_ptr->next != NULL)
{
list_b_len ++;
list_b_ptr = list_b_ptr->next;
}
if(list_a_len>list_b_len) //if a list is longer than b list, extend the b list's bits to make it has the same length of bits as list a
{
for(int j=0; j<list_a_len-list_b_len; j++)
{
list_b_ptr->next = new ListNode(0);
list_b_ptr = list_b_ptr->next;
}
}else
{
for(int j=0; j<list_b_len-list_a_len; j++)
{
list_a_ptr->next = new ListNode(0);
list_a_ptr = list_a_ptr->next;
}
}
list_a_ptr = list_a;
list_b_ptr = list_b;
bool count = false;
int carry = 0;
ListNode * list_c = new ListNode(-1);
ListNode * list_c_ptr = list_c;
while(list_a_ptr!=NULL&&list_b_ptr!=NULL)
{
carry = count+list_a_ptr->val+list_b_ptr->val; //if count is false(0), 5+9=14
list_c_ptr->next = new ListNode(carry%10); //if 14%10=4
count=carry>=10?true:false; //carry=14>=10, that count gets true(1), the next bit will carry 1
list_a_ptr=list_a_ptr->next;
list_b_ptr=list_b_ptr->next;
list_c_ptr=list_c_ptr->next;
}
if(count)
{
list_c_ptr->next = new ListNode(1);
list_c_ptr=list_c_ptr->next;
}
return list_c->next;
}
};
comment:
32 ms | 10.2 MB | Cpp |
One trouble I jumpped in is at:
while(list_a_ptr->next != NULL)
{
list_a_len ++;
list_a_ptr = list_a_ptr->next;
}
I accidently typed like this: "while(list_a_ptr != NULL)". Then "list_a_ptr = list_a_ptr->next;" makes the pointer points to NULL. Later in "list_b_ptr->next = new ListNode(0);" it will create a node after a NULL node, that's the problem.
As return list_c->next, cuz of I initiate the list_c with -1, so i got to return list_c->next as the first value(the initiated number -1 is none of this question's business).
Optimized code:
class Solution
{
public:
ListNode* addTwoNumbers(ListNode* list_a, ListNode* list_b)
{
ListNode * list_c = new ListNode(-1);
ListNode * list_c_ptr = list_c;
int the_sum = 0;
bool carry = false;
while(list_a !=NULL || list_b != NULL)
{
the_sum = 0;
if(list_a !=NULL)
{
the_sum += list_a->val;
list_a=list_a->next;
}
if(list_b !=NULL)
{
the_sum += list_b->val;
list_b=list_b->next;
}
the_sum += carry;
list_c_ptr->next = new ListNode(the_sum%10);
list_c_ptr = list_c_ptr->next;
carry = the_sum>=10?true:false;
}
if(carry)
{
list_c_ptr->next = new ListNode(1);
}
return list_c->next;
}
};
comment:
24 ms | 10.2 MB | C++ |
I typically removed the extending of list a or b.