leetcode 5. 两个链表逐个元素相加 Add Two Numbers

7问题:Add Two Numbers 难度-Medium

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

解答:

目标:做一个带进位的加法器(243+564 = 708)

本题需要重点考虑的是–两个链表不等长的情况 和 最后的进位

class Solution {
public:

    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        if (l1 == NULL || l2 == NULL)
           return l1 == NULL ? l2 : l1;
        ListNode dummy(-1); //虚拟节点,指向链表头
        ListNode* l = &dummy;
        int carry = 0;
        int v1, v2, sum;

        while (l1 || l2) {
            if (l1) {
                v1 = l1->val;
                l1 = l1->next;
            } else {
                v1 = 0;
            }
            if (l2) {
                v2 = l2->val;
                l2 = l2->next;
            } else {
                v2 = 0;
            }

            sum = v1 + v2 + carry;
            l->next = new ListNode(sum % 10);//后插法,如果是C语言,写一个后插函数
            carry = sum / 10;
            l = l->next;
        }
        //l1,l2均达到尾部时,处理最后的进位
        if (carry) {
            l->next = new ListNode(carry);
        }

        return dummy.next;
    }
};
时间: 2024-08-25 08:41:10

leetcode 5. 两个链表逐个元素相加 Add Two Numbers的相关文章

LeetCode 2:两数相加 Add Two Numbers

?给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字.如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和. You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes cont

[LeetCode]求两个链表的焦点--Intersection of Two Linked Lists

标题题目地址 1.解题意 求解两个链表的焦点,这个交点并不是焦点的值相等,而是需要交点之后的数据是完全相等的. 落实到java层面,就是交点处的对象是同一个对象即可. ps:我最开始没有读懂题目,然后就开始比较节点的值是否相等,直到示例跑失败了才知道原因. 2.通用解法 public ListNode getIntersectionNode(ListNode headA, ListNode headB) { if(headA!=null && headB!=null){ ListNode

LeetCode.2-两个数字相加(Add Two Numbers)

这是悦乐书的第340次更新,第364篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Medium级别的第1题(顺位题号是2).给定两个非空链表,表示两个非负整数. 数字以相反的顺序存储,每个节点包含一个数字.将两个数字相加并将其作为链表返回.你可以假设这两个数字不包含任何前导零,除了数字0本身.例如: 输入:(2 -> 4 -> 3)+(5 -> 6 -> 4) 输出:7 -> 0 -> 8 说明:342 + 465 = 807. 本次解题使用的开发工具是

简单的两数之和再次乱入<< 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 ->

(LeetCode)两个链表的第一个公共节点

LeetCode上面的题目例如以下: Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin to intersect at node c1. Notes: If the two lin

2.两数相加(Add Two Numbers)

第一想法是顺着题目的原因,将两链表分别转化为一个数字,再将数字相加,然后把结果转化为字符串,存到答案链表中.但是数据太大会溢出! 所以,要在计算一对数字的过程当中直接存储一个结果,注意结果大于9时进位,删去最终链表的最后一个节点. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} *

LeetCode 445. 两数相加 II(Add Two Numbers II)

445. 两数相加 II 445. Add Two Numbers II 题目描述 给定两个非空链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储单个数字.将这两数相加会返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 进阶: 如果输入链表不能修改该如何处理?换句话说,你不能对列表中的节点进行翻转. LeetCode445. Add Two Numbers II中等 示例: 输入: (7 -> 2 -> 4 -> 3) + (5 ->

【剑指offer】两个链表的第一个公共结点

转载请注明出处:http://blog.csdn.net/ns_code/article/details/26097395 简单题,剑指offer上的第37题,九度OJ上AC. 题目描述: 输入两个链表,找出它们的第一个公共结点. 输入: 输入可能包含多个测试样例.对于每个测试案例,输入的第一行为两个整数m和n(1<=m,n<=1000):代表将要输入的两个链表的元素的个数.接下来的两行,第一行为第一个链表的所有元素,中间用空格隔开.第二行为第二个链表的所有元素,中间用空格隔开. 输出: 对应

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