LeetCode 2 - Add Two Numbers

原创 2015年09月06日 12:28:56

Add Two Numbers

题目链接

https://leetcode.com/problems/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

解题思路

创建一个新链表作为返回,同时遍历L1和L2,手工实现十进制加法,结果采用尾插法写入新链表

Tips:
1. L1和L2可能长度不同,需对空结点进行处理
2. 最后一位可能产生进位,需单独处理

代码实现(C++)

 /**
 * 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* L = new ListNode(-1);     // 新链表头结点
        ListNode* p = L;    // 工作指针

        int ca = 0;    // 进位标记
        int v1, v2, sumValue, realValue;

        while (l1 || l2) {
            v1 = l1 ? l1->val : 0;
            v2 = l2 ? l2->val : 0;
            sumValue = v1 + v2 + ca;

            realValue = sumValue % 10;
            ca = sumValue / 10;

            l1 = l1 ? l1->next : nullptr;
            l2 = l2 ? l2->next : nullptr;

            p->next = new ListNode(realValue);
            p = p -> next;
        }

        if (ca > 0) {
            p -> next = new ListNode(ca);
        }

        return L -> next;
    }
};

效率

对两个链表均只遍历一遍,时间复杂度: O(n)
需要生成新的链表,空间复杂度: O(n)

相关文章推荐

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

LeetCode Algorithms 2. Add Two Numbers 题解

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

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.题目描述                                                                                              ...

算法设计Week1 LeetCode Algorithms Problem #2 Add Two Numbers

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

leetCode 2 Add Two Numbers

问题:Add Two Numbers You are given two linked lists representing two non-negative numbers. The digits...

Leetcode 2. Add Two Numbers

leetcode页面样式换了,标题和total accepted也截不到了。就直接粘描述吧 You are given two linked lists representing two non-n...

leetcode #2 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 linked lists representing two non-negative numbers. The digits are stored...

Leetcode #2 Add Two Numbers 解题小结

1 题外话我又回来更新Leetcode了,现在准备有空做做leetcode,等着用java刷完以后再切换到其他语言试试吧~~。 2 题目理解[原题](https://leetcode.com/prob...
  • MebiuW
  • MebiuW
  • 2016年03月05日 15:40
  • 656
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode 2 - Add Two Numbers
举报原因:
原因补充:

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