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 2. Add Two Numbers C语言实现

leetcode 2. Add Two Numbers Description You are given two non-empty linked lists represent...
  • banana1006034246
  • banana1006034246
  • 2017-03-31 19:48:34
  • 1229

[LeetCode][2]Add Two Numbers解析 -Java实现

Q: You are given two linked lists representing two non-negative numbers. The digits are stored in re...
  • u014629433
  • u014629433
  • 2016-06-01 16:34:44
  • 2177

【python】【leetcode】【算法题目2—Add Two Numbers】

一、题目描述 题目原文:  You are given two linked lists representing two non-negative numbers. ...
  • u014615155
  • u014615155
  • 2016-11-21 12:05:45
  • 2324

【LeetCode】2. Add Two Numbers 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51820937Subject 出处:https://leetcode.com/pro...
  • crazy1235
  • crazy1235
  • 2016-10-24 19:46:34
  • 3728

[LeetCode#2][C]Add Two Numbers

题目如下: 思路: 其实就是基本的链表操作,只要注意进位的判断就好。代码:/** * Definition for singly-linked list. * struct ListNode ...
  • FaaronZheng
  • FaaronZheng
  • 2016-05-01 19:30:16
  • 501

Add Two Numbers -- LeetCode

原题链接: http://oj.leetcode.com/problems/add-two-numbers/  这道题比较简单,是cc150里面的题,思路很明确,就是按照位数读下去,维护当前位和进位...
  • linhuanmars
  • linhuanmars
  • 2014-02-26 06:46:10
  • 12196

[LeetCode] 002. Add Two Numbers (Medium) (C++/Java/Python)

002.Add_Two_Numbers (Medium)
  • hcbbt
  • hcbbt
  • 2015-02-27 12:43:08
  • 5893

LeetCode 2. Add Two Numbers(C++)

使用了O(1)的额外空间,思路是将两个链表遍历并合并,合并过程中不断计算进位,如果计算到最后一次加法仍有进位(可能是两个链表对应位置值相加产生进位或者上一次的进位加本次值又产生进位),那么需要加一个值...
  • v_xchen_v
  • v_xchen_v
  • 2017-03-21 20:11:37
  • 405

【LeetCode】Add Two Numbers Go语言实现

问题描述You are given two linked lists representing two non-negative numbers. The digits are stored in r...
  • tracker_w
  • tracker_w
  • 2016-11-23 11:14:19
  • 933

leetcode 2:Add Two Numbers(C语言)

/**  * Definition for singly-linked list.  * struct ListNode {  *     int val;  *     struct Lis...
  • jbddygb
  • jbddygb
  • 2016-09-10 00:52:57
  • 899
收藏助手
不良信息举报
您举报文章:LeetCode 2 - Add Two Numbers
举报原因:
原因补充:

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