my raw answers for leetcode - 2. Add Two Numbers

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 ms10.2 MBCpp

 

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 ms10.2 MBC++

 

I typically removed the extending of list a or b.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值