No.002: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.
Example:
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

官方难度:

Medium

翻译:

现有2个链表结构,存储非0的数字,每个节点的数字都是个位数的而且倒着排序,把这2个链表的数字相加并倒序输出。

例子:

链表一:2->4->3

链表二:5->6->4

输出:7->0->8

思路:

1.Java中的链表结构,就是LinkedList。

2.Java中的集合迭代器ListIterator,拥有自末尾向前遍历的特性,在这里可以运用。

解题中可能遇到的困难:

1.创建ListIterator的时候,指针的位置需要指向链表的末尾。

解题代码:

 1     private static List<Integer> method(List<Integer> list1, List<Integer> list2) {
 2         int sum1 = 0;
 3         int sum2 = 0;
 4         int size1 = list1.size();
 5         int size2 = list2.size();
 6         // 声明2个ListIterator,可以逆向迭代,指针位置先给到最后一位
 7         ListIterator<Integer> iter1 = list1.listIterator(size1);
 8         ListIterator<Integer> iter2 = list2.listIterator(size2);
 9         // 逆序遍历+累加
10         while (iter1.hasPrevious()) {
11             sum1 += iter1.previous() * Math.pow(10, --size1);
12         }
13         while (iter2.hasPrevious()) {
14             sum2 += iter2.previous() * Math.pow(10, --size2);
15         }
16         int sum = sum1 + sum2;
17         List<Integer> resultList = new LinkedList<>();
18         while (sum > 0) {
19             // 取末尾
20             resultList.add(sum % 10);
21             // 去末尾
22             sum /= 10;
23         }
24         return resultList;
25     }

测试代码地址:

https://github.com/Gerrard-Feng/LeetCode/blob/master/LeetCode/src/com/gerrard/algorithm/medium/Q002.java

LeetCode题目地址:

https://leetcode.com/problems/add-two-numbers/

PS:如有不正确或提高效率的方法,欢迎留言,谢谢!

时间: 2024-10-28 10:56:39

No.002:Add Two Numbers的相关文章

LeetCode4: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:Add Two Numbers - 两个链表逐项做带进位的加法生成新链表

1.题目名称 Add Two Numbers (两个链表逐项做带进位的加法生成新链表) 2.题目地址 https://leetcode.com/problems/add-two-numbers/ 3.题目内容 英文: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

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

LeetCode --- 2. Add Two Numbers

题目链接: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

Add Two Numbers

题目: 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. 程序设计 链表节点定义 Def

[C++]LeetCode: 108 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 5. 两个链表逐个元素相加 Add Two Numbers

7问题:Add Two Numbers 难度-Medium 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 -

[Lintcode] Add Two Numbers I &amp;&amp; II

Add Two Numbers You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in reverse order, such that the 1's digit is at the head of the list. Write a function that adds the two numbers and ret

【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(