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

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

LeetCode 445. Add Two Numbers II--两个链表均按照由尾部到头部计算两个结点数值之和,保持进位

You are given two non-empty linked lists representing two non-negative integers. The most significan...

2. Add Two Numbers官方通过版本(8.46%)

public class AddTwoNumbers { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { Lis...
  • ithouse
  • ithouse
  • 2016年01月05日 15:58
  • 210

Leetcode: Add Two Numbers (2)

You are given two linked lists representing two non-negative numbers. The digits are stored in orde...
  • lzmaths
  • lzmaths
  • 2013年12月18日 14:19
  • 401

[Amazon] Add Two Numbers I(II)

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

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

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

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

【Leetcode】445. Add Two Numbers II

445. Add Two Numbers II You are given two non-empty linked lists representing two non-negative ...

[CrackCode] 2.4 Add two numbers and return the sum as a linked list

You have two numbers represented by a linked list, where each node contains a single digit The digit...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Add Two Numbers
举报原因:
原因补充:

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