自学内容网 自学内容网

LeetCode 445.两数相加||

1.题目要求:

给你两个 非空 链表来代表两个非负整数。数字最高位位于链表开始位置。它们的每个节点只存储一位数字。将这两数相加会返回一个新的链表。

你可以假设除了数字 0 之外,这两个数字都不会以零开头。

 

示例1:



输入:l1 = [7,2,4,3], l2 = [5,6,4]
输出:[7,8,0,7]
示例2:

输入:l1 = [2,4,3], l2 = [5,6,4]
输出:[8,0,7]
示例3:

输入:l1 = [0], l2 = [0]
输出:[0]
 

提示:

链表的长度范围为 [1, 100]
0 <= node.val <= 9
输入数据保证链表代表的数字无前导 0

2.做题步骤:
1.遍历两个链表,得到它们的节点数:

 struct ListNode* cur1 = l1;
    struct ListNode* cur2 = l2;
    int count_1 = 0;
    int count_2 = 0;
    while(cur1)
    {
        count_1++;
        cur1 = cur1->next;
    }
    while(cur2)
    {
        count_2++;
        cur2 = cur2->next;
    }

2.然后创造两个数组,把两个链表的值放入两个数组中:

int* number1 = (int*)malloc(sizeof(int) * count_1);
    int* number2 = (int*)malloc(sizeof(int) * count_2);
    cur1 = l1;
    cur2 = l2;
    int i_1 = 0;
    int i_2 = 0;
    while(cur1)
    {
        number1[i_1] = cur1->val;
        cur1 = cur1->next;
        i_1++;
    }
    while(cur2){
        number2[i_2] = cur2->val;
        i_2++;
        cur2 = cur2->next;
    }

3.然后再判断两个数组的长度的,把两个数组相加的值,放入长的数组:

if(count_1 > count_2)
    {
        int i = 0;
        for(i = 0;i < count_1;i++)
        {
            if(count_1 - i == count_2)
            {
                break;
            }
        }
        int j = 0;
        for(j = 0;j < count_2;j++)
        {
            number1[i] += number2[j];
            i++;
        }
        i = count_1 - 1;
        for(i = count_1 - 1;i > 0;i--){
            if(number1[i] > 9)
            {
                number1[i - 1] += 1;
                number1[i] %= 10;
            }
        }
        if(number1[0] > 9)
        {
            struct ListNode* head = (struct ListNode*)malloc(sizeof(struct ListNode));
            head->next = l1;
            l1 = head;
            cur1 = l1;
            cur1->val = number1[0] / 10;
            cur1 = cur1->next;
            cur1->val = number1[0] % 10;
            cur1 = cur1->next;
            for(i = 1;i < count_1;i++)
            {
                cur1->val = number1[i];
                cur1 = cur1->next;
            }
            return l1;
        }else{
            cur1 = l1;
            for(i = 0;i < count_1;i++)
            {
                cur1->val = number1[i];
                cur1 = cur1->next;
            }
            return l1;
        }
    }else{
        int i = 0;
        for(i = 0;i < count_2;i++)
        {
            if(count_2 - i == count_1)
            {
                break;
            }
        }
        int j = 0;
        for(j = 0;j < count_1;j++)
        {
            number2[i] += number1[j];
            i++;
        }
        i = count_2 - 1;
        for(i = count_2 - 1;i > 0;i--)
        {
            if(number2[i] > 9)
            {
                number2[i - 1] += 1;
                number2[i] %= 10;
            }
        }
        if(number2[0] > 9)
        {
            struct ListNode* head = (struct ListNode*)malloc(sizeof(struct ListNode));
            head->next = l2;
            l2 = head;
            cur2 = l2;
            cur2->val = number2[0] / 10;
            cur2 = cur2->next;
            cur2->val = number2[0] % 10;
            cur2 = cur2->next;
            for(i = 1;i < count_2;i++)
            {
                cur2->val = number2[i];
                cur2 = cur2->next;
            }
            return l2;
        }else{
            cur2 = l2;
            for(i = 0;i < count_2;i++)
            {
                cur2->val = number2[i];
                cur2 = cur2->next;
            }
            return l2;
        }
    }

全部代码:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){
    struct ListNode* cur1 = l1;
    struct ListNode* cur2 = l2;
    int count_1 = 0;
    int count_2 = 0;
    while(cur1)
    {
        count_1++;
        cur1 = cur1->next;
    }
    while(cur2)
    {
        count_2++;
        cur2 = cur2->next;
    }
    int* number1 = (int*)malloc(sizeof(int) * count_1);
    int* number2 = (int*)malloc(sizeof(int) * count_2);
    cur1 = l1;
    cur2 = l2;
    int i_1 = 0;
    int i_2 = 0;
    while(cur1)
    {
        number1[i_1] = cur1->val;
        cur1 = cur1->next;
        i_1++;
    }
    while(cur2){
        number2[i_2] = cur2->val;
        i_2++;
        cur2 = cur2->next;
    }
    if(count_1 > count_2)
    {
        int i = 0;
        for(i = 0;i < count_1;i++)
        {
            if(count_1 - i == count_2)
            {
                break;
            }
        }
        int j = 0;
        for(j = 0;j < count_2;j++)
        {
            number1[i] += number2[j];
            i++;
        }
        i = count_1 - 1;
        for(i = count_1 - 1;i > 0;i--){
            if(number1[i] > 9)
            {
                number1[i - 1] += 1;
                number1[i] %= 10;
            }
        }
        if(number1[0] > 9)
        {
            struct ListNode* head = (struct ListNode*)malloc(sizeof(struct ListNode));
            head->next = l1;
            l1 = head;
            cur1 = l1;
            cur1->val = number1[0] / 10;
            cur1 = cur1->next;
            cur1->val = number1[0] % 10;
            cur1 = cur1->next;
            for(i = 1;i < count_1;i++)
            {
                cur1->val = number1[i];
                cur1 = cur1->next;
            }
            return l1;
        }else{
            cur1 = l1;
            for(i = 0;i < count_1;i++)
            {
                cur1->val = number1[i];
                cur1 = cur1->next;
            }
            return l1;
        }
    }else{
        int i = 0;
        for(i = 0;i < count_2;i++)
        {
            if(count_2 - i == count_1)
            {
                break;
            }
        }
        int j = 0;
        for(j = 0;j < count_1;j++)
        {
            number2[i] += number1[j];
            i++;
        }
        i = count_2 - 1;
        for(i = count_2 - 1;i > 0;i--)
        {
            if(number2[i] > 9)
            {
                number2[i - 1] += 1;
                number2[i] %= 10;
            }
        }
        if(number2[0] > 9)
        {
            struct ListNode* head = (struct ListNode*)malloc(sizeof(struct ListNode));
            head->next = l2;
            l2 = head;
            cur2 = l2;
            cur2->val = number2[0] / 10;
            cur2 = cur2->next;
            cur2->val = number2[0] % 10;
            cur2 = cur2->next;
            for(i = 1;i < count_2;i++)
            {
                cur2->val = number2[i];
                cur2 = cur2->next;
            }
            return l2;
        }else{
            cur2 = l2;
            for(i = 0;i < count_2;i++)
            {
                cur2->val = number2[i];
                cur2 = cur2->next;
            }
            return l2;
        }
    }
}

好了,这就是我的代码,大家如果觉得好的话,就给个免费的赞吧,谢谢各位大佬们了 ^ _ ^ .


原文地址:https://blog.csdn.net/m0_54244065/article/details/140373799

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!