LeetCode || Add Two Numbers

原创 2015年07月10日 09:37:21

运行时间为40ms的代码如下(时间有点长):

/**
 * 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 *p1, *p2, *p3;
        p1 = l1;
        p2 = l2;
        int output = 0;
        ListNode *head = new ListNode(0);
        head->next = NULL;
        p3 = head;
        int newDigital;
        int total;
        for(; p1 != NULL && p2 != NULL; ) {
            total = p1->val + p2->val + output;
            if(total >= 10) {
                newDigital = total - 10;
                output = 1;
            }else {
                newDigital = total;
                output = 0;
            }
            p3->next = new ListNode(newDigital);
            p1 = p1->next;
            p2 = p2->next;
            p3 = p3->next;
        }
        if(p1 == NULL) {
            for(; p2 != NULL; ) {
                total = p2->val + output;
                if(total >= 10) {
                    newDigital = total - 10;
                    output = 1;
                }else {
                    newDigital = total;
                    output = 0;
                }
                p3->next = new ListNode(newDigital);
                p2 = p2->next;
                p3 = p3->next;
            }
        }

        if(p2 == NULL) {
            for(; p1 != NULL; ) {
                total = p1->val + output;
                if(total >= 10) {
                    newDigital = total - 10;
                    output = 1;
                }else {
                    newDigital = total;
                    output = 0;
                }
                p3->next = new ListNode(newDigital);
                p1 = p1->next;
                p3 = p3->next;
            }
        }

        if(output == 1) {
            p3->next = new ListNode(1);
            p3 = p3->next;
        }

        p3->next = NULL;

        return head->next;


    }
};

和大神们学习每天一题(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...

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

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

【Leetcode】445. Add Two Numbers II

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

LeetCode Algorithms 2. Add Two Numbers 题解

题目: You are given two non-empty linked lists representing two non-negative integers. The digits...

leetcode3--Add Two Numbers

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

LeetCode#2 Add Two Numbers

题目:You are given two non-empty linked lists representing two non-negative integers. The digits are s...

leetcode 2. Add Two Numbers

1.题目描述                                                                                              ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode || Add Two Numbers
举报原因:
原因补充:

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