[算法练习]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

程序代码:

#include <gtest/gtest.h>

using namespace std;

struct ListNode {

    int val;
    ListNode *next;
    ListNode(int x) :
        val(x), next(NULL)
    {}
};

ListNode* addTwoNumbers(ListNode* l1, ListNode* l2)
{
    ListNode* result = 0;
    ListNode* current = 0;
    int carry = 0;

    while (l1 != NULL || l2 != NULL)
    {
        int value = 0;
        if (l1 != NULL)
        {
            value += l1->val;
            l1 = l1->next;
        }

        if (l2 != NULL)
        {
            value += l2->val;
            l2 = l2->next;
        }

        if (current == NULL)
        {
            current = result = new ListNode(value % 10);
            carry = value / 10;
        }
        else
        {
            current->next = new ListNode((value + carry) % 10);
            carry = (value + carry) / 10;
            current = current->next;
        }
    }

    if (carry)
    {
        if (current == NULL)
        {
            current = result = new ListNode(carry);
        }
        else
        {
            current->next = new ListNode(carry);
            current = current->next;
        }
    }

    return result;
}

TEST(Pratices, tAddTwoNumbers)
{
    // (2 -> 4 -> 3) + (5 -> 6 -> 4)  7 -> 0 -> 8
    ListNode* l1 = new ListNode(2);
    l1->next = new ListNode(4);
    l1->next->next = new ListNode(3);

    ListNode* l2 = new ListNode(5);
    l2->next = new ListNode(6);
    l2->next->next = new ListNode(4);

    ListNode* result = addTwoNumbers(l1,l2);

    // NULL + (2 -> 4 -> 3)
    result = addTwoNumbers(NULL,l1);

    // (2 -> 4 -> 3) + (5 -> 6 -> 4 -> 4)
    l2->next->next->next = new ListNode(4);
    result = addTwoNumbers(l1,l2);

    // 1 + 9->9
    l1 = new ListNode(1);
    l2 = new ListNode(9);
    l2->next = new ListNode(9);

    result = addTwoNumbers(l1,l2);
}
时间: 2024-10-05 18:16:56

[算法练习]Add Two Numbers的相关文章

【数据结构】算法 LinkList (Add Two Numbers)

两个用链表代表的整数,其中每个节点包含一个数字.数字存储按照在原来整数中相反的顺序,使得第一个数字位于链表的开头.写出一个函数将两个整数相加,用链表形式返回和. Solution:建立一个新链表C,然后把输入的两个链表从头往后查,每两个相加,添加一个新节点到新链表C后面, 问题注意点1就是要进位问题.2. 最高位的进位问题要最后特殊处理 . public ListNode addLists(ListNode l1, ListNode l2) { ListNode dummy = new List

leetcode速度才是王道 2. Add Two Numbers

2. 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)

我的LC之路-2. Add Two Numbers

写在前面:在LeetCode上做的题,代码基本都是我自己写的,效率有好有坏,仅供参考.有少数几道题苦思冥想做不出来的,可能会借鉴网上大神的解法,然后再自己编写,借鉴过他人解法的题会有说明.本人不是算法出身,语言基本靠自学,有任何错误理解或者不当举措,还请各位大侠手下留情并不吝赐教!万分感谢~ 依旧先把题搬一下: 2. Add Two Numbers You are given two non-empty linked lists representing two non-negative int

2、Add Two Numbers

1.Add Two Numbers--这是leedcode的第二题: 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:

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 ->

Add Two Numbers(Linked List)

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) + (

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. 程序设计 链表节点定义 Def

Leetcode 02 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) + (