[C++]LeetCode: 108 Add Two Numbers (反序链表求和)

题目:

You are given two linked lists representing two non-negative numbers. 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.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)

Output: 7 -> 0 -> 8

思路:

首先要理解题目,输入的两个链表都是反序存储的。也就是链表首位代表数字的个位,第二位代表数字百位。接下来就和一般求和的题目一样了,维护当前求和结果和进位即可。注意最后要判断,是否还有进位,如果有,还需要再生成一位。

Attention:

1. 如何生成一个链表。维护一个头结点。同时,不断链接下去。最后返回头结点的下一个节点即可。

ListNode* d = new ListNode(0);
ListNode* head = d;
d->next = new ListNode(tmp % 10);
d = d->next;
return head->next;

复杂度:O(N)

AC 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 *l1, ListNode *l2) {
        //链表中的数字都是反向存储的,链表首位是个位,第二位是百位,依次类推

        ListNode* c1 = l1;
        ListNode* c2 = l2;
        ListNode* d = new ListNode(0);
        ListNode* head = d;
        int carry = 0;

        while(c1 != NULL || c2 != NULL)
        {
            int sum = 0;
            if(c1 != NULL)
            {
                sum += c1->val;
                c1 = c1->next;
            }
            if(c2 != NULL)
            {
                sum += c2->val;
                c2 = c2->next;
            }
            int tmp = sum + carry;
            d->next = new ListNode(tmp % 10);
            d = d->next;
            carry = tmp / 10;
        }
        //如果还有进位,加一位
        if(carry != 0)
            d->next = new ListNode(carry);

        return head->next;
    }
};

这道题还可以有一些拓展内容: Add
Two Numbers -- LeetCode

比如这个其实是BigInteger的相加,数据结果不一定要用链表,也可以是数组,面试中可能两种都会问而且实现。然后接下来可以考一些OO设计的东西,比如说如果这是一个类应该怎么实现,也就是把数组或者链表作为成为成员变量,再把这些操作作为成员函数,进一步的问题可能是如何设计constructor,这个问题除了基本的还得对内置类型比如int,
long的constructor, 类似于BigInteger(int num), BigInteger(int long). 总体来说问题还是比较简单,但是这种问题不能出错,所以还是要谨慎对待。

时间: 2024-12-17 22:24:38

[C++]LeetCode: 108 Add Two Numbers (反序链表求和)的相关文章

LeetCode:Add Two Numbers - 两个链表逐项做带进位的加法生成新链表

1.题目名称 Add Two Numbers (两个链表逐项做带进位的加法生成新链表) 2.题目地址 https://leetcode.com/problems/add-two-numbers/ 3.题目内容 英文:You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a

LeetCode --- 2. Add Two Numbers

题目链接:Add Two Numbers You are given two linked lists representing two non-negative numbers. 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. Input: (2 -> 4 -> 3

【LeetCode】Add Two Numbers

You are given two linked lists representing two non-negative numbers. 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. Input: (2 -> 4 -> 3) + (5 -> 6 ->

leetcode——Add Two Numbers 两个链表表示的正整数对其求和(AC)

You are given two linked lists representing two non-negative numbers. 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. Input: (2 -> 4 -> 3) + (5 -> 6 ->

【leetcode】Add Two Numbers 解析以及拓展

题目: You are given two linked lists representing two non-negative numbers. 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. Input: (2 -> 4 -> 3) + (5 -> 6 -&

leetcode -day20 Add Two Numbers

1.  Add Two Numbers You are given two linked lists representing two non-negative numbers. 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. Input: (2 -> 4 ->

LeetCode OJ - Add Two Numbers

题目: You are given two linked lists representing two non-negative numbers. 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. Input: (2 -> 4 -> 3) + (5 -> 6 -&

[Leetcode]Add Two Numbers

题目 You are given two linked lists representing two non-negative numbers. 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. Input: (2 -> 4 -> 3) + (5 -> 6 -&g

leetcode 2 Add Two Numbers 方法2

You are given two linked lists representing two non-negative numbers. 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. Input: (2 -> 4 -> 3) + (5 -> 6 ->