445.两数相加

题目描述:

给你两个 非空 链表来代表两个非负整数。数字最高位位于链表开始位置。它们的每个节点只存储一位数字。将这两数相加会返回一个新的链表。
你可以假设除了数字 0 之外,这两个数字都不会以零开头。

示例:

来源:力扣(LeetCode)
来源:力扣(LeetCode)
来源:力扣(LeetCode)

解题思路:

创建数组–》将链表转换为数值进行加法运算,从数组后面进行判断,若大于十,将前一位进行加一,该数值减十,最后将数组转换为链表。

代码附上:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int x=size(l1),y=size(l2),i=1,j=0;
        int[] data=new int[x>=y?x+1:y+1];
        ListNode head=null,l3=null,l4=null;;
        if(x>=y) {
            l3=l1;
            l4=l2;
        } else {
            l3=l2;
            l4=l1;
        }
        while(l3!=null) {
            data[i]=l3.val;
            i++;
            l3=l3.next;
        }
        j=data.length-size(l4);
        while(l4!=null) {
            data[j]+=l4.val;
            j++;
            l4=l4.next;
        }  
        for(int k=data.length-1;k>0;k--) {
            if(data[k]>=10) {
                data[k-1]+=1;
                data[k]-=10;
            }
        }
        if(data[0]!=0) {
            if(x>=y) {
                head=new ListNode(data[0],l1);
                l3=l1;
            }
            else {
                head=new ListNode(data[0],l2);
                l3=l2;
            }
        } else {
            if(x>=y) {
                head=l1;
                l3=l1;
            } else {
                head=l2;
                l3=l2;
            }
        }
        j=1;
        while(l3!=null) {
            l3.val=data[j];
            j++;
            l3=l3.next;
        }
        return head;
    }
    public int size(ListNode text) {
        int x=0;
        while(text!=null) {
            x++;
            text=text.next;
        }
        return x;
    } 
}
给定的参考引用中未包含力扣第445题的C语言解决方案。力扣第445题是“两数相加 II”,以下是一种常见的C语言解法思路及代码示例: 思路:可以先将两个链表反转,然后对反转后的链表进行逐位相加,最后再将结果链表反转回来。 ```c #include <stdio.h> #include <stdlib.h> // 定义链表节点结构 struct ListNode { int val; struct ListNode *next; }; // 反转链表函数 struct ListNode* reverseList(struct ListNode* head) { struct ListNode *prev = NULL; struct ListNode *curr = head; struct ListNode *next = NULL; while (curr != NULL) { next = curr->next; curr->next = prev; prev = curr; curr = next; } return prev; } // 两数相加函数 struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) { l1 = reverseList(l1); l2 = reverseList(l2); struct ListNode *dummyHead = (struct ListNode*)malloc(sizeof(struct ListNode)); dummyHead->val = 0; dummyHead->next = NULL; struct ListNode *curr = dummyHead; int carry = 0; while (l1 != NULL || l2 != NULL || carry != 0) { int x = (l1 != NULL) ? l1->val : 0; int y = (l2 != NULL) ? l2->val : 0; int sum = carry + x + y; carry = sum / 10; struct ListNode *newNode = (struct ListNode*)malloc(sizeof(struct ListNode)); newNode->val = sum % 10; newNode->next = NULL; curr->next = newNode; curr = newNode; if (l1 != NULL) l1 = l1->next; if (l2 != NULL) l2 = l2->next; } struct ListNode *result = reverseList(dummyHead->next); free(dummyHead); return result; } // 打印链表函数 void printList(struct ListNode* head) { struct ListNode *curr = head; while (curr != NULL) { printf("%d ", curr->val); curr = curr->next; } printf("\n"); } int main() { // 创建示例链表 l1: 7 -> 2 -> 4 -> 3 struct ListNode *l1 = (struct ListNode*)malloc(sizeof(struct ListNode)); l1->val = 7; l1->next = (struct ListNode*)malloc(sizeof(struct ListNode)); l1->next->val = 2; l1->next->next = (struct ListNode*)malloc(sizeof(struct ListNode)); l1->next->next->val = 4; l1->next->next->next = (struct ListNode*)malloc(sizeof(struct ListNode)); l1->next->next->next->val = 3; l1->next->next->next->next = NULL; // 创建示例链表 l2: 5 -> 6 -> 4 struct ListNode *l2 = (struct ListNode*)malloc(sizeof(struct ListNode)); l2->val = 5; l2->next = (struct ListNode*)malloc(sizeof(struct ListNode)); l2->next->val = 6; l2->next->next = (struct ListNode*)malloc(sizeof(struct ListNode)); l2->next->next->val = 4; l2->next->next->next = NULL; struct ListNode *result = addTwoNumbers(l1, l2); printList(result); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值