[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 -> 4)
Output: 7 -> 0 -> 8

解题思路

从链表头部向尾部逐一相加,注意进位即可。

实现代码

// Runtime: 40 ms
/**
 * 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 *newHead = new ListNode(0);
        ListNode *temp = newHead;
        int sum = 0;
        while (l1 != NULL || l2 != NULL)
        {
            if (l1 != NULL)
            {
                sum += l1->val;
                l1 = l1->next;
            }
            if (l2 != NULL)
            {
                sum += l2->val;
                l2 = l2->next;
            }

            temp->next = new ListNode(sum % 10);
            temp = temp->next;
            sum /= 10;
        }

        if (sum == 1)
        {
            temp->next = new ListNode(sum);
        }

        return newHead->next;
    }
};
时间: 2024-12-03 12:50:09

[LeetCode] Add Two Numbers的相关文章

leetcode 002 add two numbers C语言

问题描述 leetcode 002 add two numbers C语言 struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) { struct ListNode *l, *p; l = (struct ListNode*)malloc(sizeof(struct ListNode)); l->val = l1->val + l2->val; p = l; l1 = l1->next; l

求帮助:2. Add Two Numbers ,已疯,谢谢

问题描述 求帮助:2. Add Two Numbers ,已疯,谢谢 自己运行: 提交以后: 代码: /** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) { struct ListNode *l,

[LeetCode] Self Dividing Numbers 自整除数字

A self-dividing number is a number that is divisible by every digit it contains. For example, 128 is a self-dividing number because 128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0. Also, a self-dividing number is not allowed to contain the digit zero. G

LeetCode 2 Add Two Numbers

翻译: 给你两个表示两个非负数字的链表.数字以相反的顺序存储,其节点包含单个数字.将这两个数字相加并将其作为一个链表返回. 输入: (2 -> 4 -> 3) + (5 -> 6 -> 4) 输出: 7 -> 0 -> 8 原题: You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of

[LeetCode] Compare Version Numbers

Compare two version numbers version1 and version2. If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. You may assume that the version strings are non-empty and contain only digits and the . character. The . charac

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 Digits - 数字各个位数求和

题目概述:Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.For example:Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it.Follow up:Could you do it without a

[LeetCode] Add Digits

Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. For example: Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it. Follow up: Could you do it without an

leetcode难度及面试频率

转载自:LeetCode Question Difficulty Distribution               1 Two Sum 2 5 array sort         set Two Pointers 2 Add Two Numbers 3 4 linked list Two Pointers           Math 3 Longest Substring Without Repeating Characters 3 2 string Two Pointers