两数相加(m)

给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。

如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。

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

示例:

输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
原因:342 + 465 = 807

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/add-two-numbers
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。


方法一:迭代

1.设置虚拟头结点:为防止内存泄露,虚拟头结点需删除

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){
    int c = 0;
    struct ListNode *head = (struct ListNode *)malloc(sizeof(struct ListNode)), *cur = head, *del = head;
    //head虚拟头结点地址,cur当前结点地址,del用于删除虚拟头结点
    while(l1 != NULL || l2 != NULL || c)
    {
        cur->next = (struct ListNode *)malloc(sizeof(struct ListNode));
        cur = cur->next;
        l1 = l1 != NULL?(c+=l1->val,l1->next):l1;
        l2 = l2 != NULL?(c+=l2->val,l2->next):l2;
        cur->val =  c%10;//输出值存入cur列表中
        c=c/10;//计算进位数值
    }
    cur->next = NULL;
    head = head->next;
    free(del);
    return head;
}

2.不设置虚拟头结点

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){
    int c = 0;
    struct ListNode *head = (struct ListNode *)malloc(sizeof(struct ListNode)), *cur = head;
    while(cur != NULL){
        l1 = l1 != NULL?(c+=l1->val,l1->next):l1;
        l2 = l2 != NULL?(c+=l2->val,l2->next):l2;
        cur->val =  c%10;//输出值存入cur列表中
        c=c/10;//计算进位数值
        cur->next = (l1 != NULL || l2 != NULL || c != 0)?(struct ListNode *)malloc(sizeof(struct ListNode)):NULL;
        cur=cur->next;
    }
    return head;
}

方法二:递归

int c = 0;
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){
    if(l1==NULL && l2==NULL && c==0) return NULL;
    l1 = l1 != NULL? (c += l1->val, l1->next):l1;
    l2 = l2 != NULL? (c += l2->val, l2->next):l2;
    struct ListNode *cur = (struct ListNode *)malloc(sizeof(struct ListNode));
    cur->val = c%10;
    c /= 10;
    cur->next = addTwoNumbers(l1,l2);
    return cur;
}

 Java:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode dummyHead = new ListNode(0);
        ListNode p = l1, q = l2, curr = dummyHead;
        int carry = 0;
        while(p != null || q != null){
            int x = (p != null)? p.val : 0;
            int y = (q != null)? q.val : 0;
            int sum = carry + x + y;
            carry = sum/10;
            curr.next = new ListNode(sum%10);
            curr = curr.next;
            if(p != null)p = p.next;
            if(q != null)q = q.next;
        }
        if(carry>0){
            curr.next = new ListNode(carry);
        }
        return dummyHead.next;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值