Add Two Numbers

原创 2015年07月08日 22:07:16

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

/**
 * 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 *output = NULL;
        ListNode *current = output;
        ListNode* start1 = l1;
        ListNode* start2 = l2;
        int up = 0;
        while(start1!= NULL || start2 != NULL)
        {
            int value = up;
            if(start1 != NULL)
                value += start1->val;
            if(start2 != NULL)
                value += start2->val;
            up = value / 10; 
            value = value % 10;
            ListNode *node = new ListNode(value);//ListNode node(value);the memory is allocated on stack, will free soon. 
            if(current == NULL)
            {
                current = node;
                output = node;
            }
            else{
                current->next = node;
                current = node;
            }
            if(start1 != NULL)
                start1 = start1->next;
            if(start2 != NULL)
                start2 = start2->next;
        }
        if(up > 0)
        {
            ListNode *node = new ListNode(up);
            current->next = node;
        }
        return output;
    }
};



版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Leetcode: Add Two Numbers (2)

You are given two linked lists representing two non-negative numbers. The digits are stored in orde...

Add Two Numbers

Add two numbes You are given two linked lists representing two non-negative numbers. The digits are ...

[leetcode NO.3] Add Two Numbers (JAVA)

You are given two linked lists representing two non-negative numbers. The digits are stored in rever...

[Amazon] Add Two Numbers I(II)

You are given two non-empty linked lists representing two non-negative integers. The digits are stor...

【LeetCode002-003算法/编程练习C++】---Add Two Numbers||寻找最长无重复字母子串。//第三题之后有空要再想一想…

leetcode第2和第3题

Add Two Numbers

这道题逻辑不难,但是对于指针和链表都搞不定的我感觉要死了..... 先说几件事,int *p=&i,这个意思就是p指向i了,p存的是i的地址,int *p; p=&i;这个和前面的意思一样..... ...

LeetCode 2 Add Two Numbers

You are giventwo linked lists representing two non-negative numbers. The digits are storedin reverse...

Add Two Numbers

/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next...

LeetCode:Add Two Numbers

这是第一次用markdown写东西啊,本来写的就少,试一试吧。 题目: You are given two linked lists representing two non-negative...

Single Number && Single Number II && Factorial Trailing Zeroes && Pow(x, n) && Add Two Numbers

Single Number 1、异或交换性质 int singleNumber(int A[], int n) { int ans = A[0]; for (i...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)