LeetCode第二题:Add Two Numbers

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.

给出两个表示两个非负整数的非空链表。整数以相反的顺序存储,它们的每个节点都包含一个数字。将两个数字相加,并将其作为链接列表返回。

你可以假设这两个数字不包含任何前导零,除了第0个数字本身。

Example

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

题目本身不难,但是一定要记得最后的进位问题。下面贴下我的代码,代码量偏多,但是我认为比较好理解。

 1  public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
 2         ListNode root = new ListNode(0);
 3         ListNode cur = root;//小技巧定义结果的上一个节点,返回时返回root.next
 4         int temp = 0;       //避免要先进行一次初值的计算
 5         while (l1 != null || l2 != null) {
 6             int n1 = 0;
 7             int n2 = 0;
 8             if (l1 != null) {//因为两个数不一定一样长,当一个数为空时,
 9                 n1 = l1.val; //用0计算即可,熟练的同学完全可以用三目运算符解决。
10                 l1 = l1.next;
11             }
12             if (l2 != null) {
13                 n2 = l2.val;
14                 l2 = l2.next;
15             }
16             ListNode node = new ListNode((n1 + n2 + temp) % 10);
17             temp = (n1 + n2 + temp) / 10;
18             cur.next = node;
19             cur = node;
20         }
21         //这段代码千万不要忘记,如果最后有进位,需要添加节点。
22         //当然简洁的代码是在while循环中while (l1 != null || l2 != null||temp!=0)
23         //在循环中解决这个问题,我单独列出来,希望大家牢记这一点,如果在面试中漏掉这种情况
24         //应该会在面试官那里减分的。
25         if (temp != 0) {
26             cur.next = new ListNode(temp);
27         }
28         return root.next;
29     }
时间: 2024-07-29 05:05:07

LeetCode第二题:Add Two Numbers的相关文章

LeetCode 第二题,Median of Two Sorted Arrays

题目再现 There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). 题意解析 题目意思是给两个大小为m,n的有序数组(m,n可能为0),要求找出这两个数组的中位数.并且程序的时间复杂度必须不能超过O(log(m+n)). 这道题的

乘风破浪:LeetCode真题_002_Add Two Numbers

乘风破浪:LeetCode真题_002_Add Two Numbers 一.前言     这次的题目是关于链表方面的题目,把两个链表对应节点相加,还要保证进位,每个节点都必须是十进制的0~9.因此主要涉及到链表,指针方面的知识,以及活学活用的编程能力. 二.LeetCode真题_002_Add Two Numbers 2.1 问题介绍 2.2 分析与解决 看到这样的问题,我们首先要分析清题意,之后画出一个原理图,然后就便于解决了.可以看到主要是包括了进位的问题,因此我们每一次相加的时候需要考虑到

【LeetCode】002 Add Two Numbers

题目:LeetCode 002 Add Two Numbers 题意:给定表达非负数的两个链表,这些数字按照反向顺序存储,每个节点包含一个单独的数字,将这两个数相加,返回一个新链表. 样例: Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 链表每个节点的结构: 1 struct ListNode { 2 int val; 3 ListNode *next; 4 ListNode(int x) : val(

LeetCode算法题-Add Strings(Java实现)

这是悦乐书的第223次更新,第236篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第90题(顺位题号是415).给定两个非负整数num1和num2表示为字符串,返回num1和num2的总和. 注意: num1和num2的长度均<5100. num1和num2都只包含数字0-9. num1和num2都不包含任何前导零. 您不能使用任何内置BigInteger库或直接将输入转换为整数. 本次解题使用的开发工具是eclipse,jdk使用的版本是1.8,环境是win7 6

leetcode_2_题——Add Two Numbers(链表)

Add Two Numbers Total Accepted: 58510 Total Submissions: 268082My Submissions Question Solution 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 digi

LeetCode OJ #2 Add Two Numbers

https://leetcode.com/problems/add-two-numbers/ 题意:两个链表表示两个数 , 2 4 3 5 6 4 按照加法的规则从左往右加以及 进位. 输出:和的链表. 仍然是水题.一些特殊情况需要考虑(这些做题的时候我考虑到了) 1.如果两个长度不等,如l1>l2  ,那么需要l1比l2多出来的部分加上进位copy到ans里 2.5+5的情况,虽然长度相等,但是最高位进位了 可惜,WA了两发,因为 正确的应该是 int v = l1->val + l2-&g

LeetCode No.2 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 ->

LeetCode Problem 2.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 ->

【Leetcode】2. Add Two Numbers

Problem: 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 tw