LeetCode 2. Add Two Numbers

原创 2018年04月17日 10:43:20

问题描述

  • You are given two non-empty linked lists representing two non-negative integers. 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.
    You may assume the two numbers do not contain any leading zero, except the number 0 itself.
  • Example :

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.

问题分析

  • 将链表相应位置相加,并注意进位问题即可

代码实现

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        if (l1 == null || l2 == null) {
            return null;
        }
        ListNode cur1 = l1;
        ListNode cur2 = l2;
        ListNode dummy = new ListNode(0);
        ListNode newTail = dummy;
        int carry = 0;
        //将两链对应元素带进位相加
        while (cur1 != null || cur2 != null) {
            int val = 0;
            if (cur1 != null) {
                val += cur1.val;
                cur1 = cur1.next;
            }
            if (cur2 != null) {
                val += cur2.val;
                cur2 = cur2.next;
            }
            newTail.next = new ListNode((val + carry) % 10);
            newTail = newTail.next;
            //进位信息
            carry = (val + carry) / 10;
        }
        //最终的进位信息
        if (carry != 0) {
            newTail.next = new ListNode(carry);
        }
        return dummy.next;
    }
        /*
        while (cur1 != null && cur2 != null) {
            int val = (cur1.val + cur2.val + carry) % 10;
            newTail.next = new ListNode(val);
            newTail = newTail.next;
            carry = (cur1.val + cur2.val + carry) / 10;
            cur1 = cur1.next;
            cur2 = cur2.next;
        }
        while (cur1 != null) {
            int val = (cur1.val + carry) % 10;
            newTail.next = new ListNode(val);
            newTail = newTail.next;
            carry = (cur1.val + carry) / 10;
            cur1 = cur1.next;
        }
         while (cur2 != null) {
            int val = (cur2.val + carry) % 10;
            newTail.next = new ListNode(val);
            newTail = newTail.next;
            carry = (cur2.val + carry) / 10;
            cur2 = cur2.next;
        }
        if (carry != 0) {
            newTail.next = new ListNode(carry);
        }
        return dummy.next;
    }
    */
}
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zjxxyz123/article/details/79971398

【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
  • 3746

[C语言][LeetCode][2]Add Two Numbers

[C语言][LeetCode][2]Add Two Numbers
  • Timsley
  • Timsley
  • 2016-04-14 21:25:50
  • 942

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

[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
  • 2180

LeetCode Add Two Numbers 之JavaScript 多种解法

LeetCode Add Two Numbers 之JavaScript 多种解法
  • weixin_36751895
  • weixin_36751895
  • 2017-07-13 12:45:21
  • 504

【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
  • 2336

[LeetCode#2][C]Add Two Numbers

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

leetcode(二)—— Add Two Numbers(Python/C++)

Add Two Numbers | LeetCode OJ 逻辑是简单的,主要是处理进位,以及两序列长度不一致的情况; Python# 链表结点定义 class ListNode(object):...
  • lanchunhui
  • lanchunhui
  • 2016-04-11 09:48:50
  • 642

[LeetCode]Add Two Numbers解析

将两个链表的值进行相加,并返回一个新的链表,注意两个地方:一是考虑两个链表的长度是否相同,二是考虑相加后的进位问题 /** * Definition for singly-linked list...
  • rudyn
  • rudyn
  • 2016-08-19 14:12:05
  • 433

LeetCode 2. Add Two Numbers(C++)

使用了O(1)的额外空间,思路是将两个链表遍历并合并,合并过程中不断计算进位,如果计算到最后一次加法仍有进位(可能是两个链表对应位置值相加产生进位或者上一次的进位加本次值又产生进位),那么需要加一个值...
  • v_xchen_v
  • v_xchen_v
  • 2017-03-21 20:11:37
  • 407
收藏助手
不良信息举报
您举报文章:LeetCode 2. Add Two Numbers
举报原因:
原因补充:

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