原题链接:http://oj.leetcode.com/problems/add-two-numbers/这道题比较简单,是cc150里面的题,思路很明确,就是按照位数读下去,维护当前位和进位,时间复杂度是O(n),空间复杂度是O(1).代码如下:
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
int carry = 0;
int digit = 0;
ListNode head = null;
ListNode pre = null;
while(l1!=null && l2!=null)
{
digit = (l1.val+l2.val+carry)%10;
carry = (l1.val+l2.val+carry)/10;
ListNode newNode = new ListNode(digit);
if(head==null)
head = newNode;
else
pre.next = newNode;
pre = newNode;
l1 = l1.next;
l2 = l2.next;
}
while(l1!=null)
{
digit = (l1.val+carry)%10;
carry = (l1.val+carry)/10;
ListNode newNode = new ListNode(digit);
if(head==null)
head = newNode;
else
pre.next = newNode;
pre = newNode;
l1 = l1.next;
}
while(l2!=null)
{
digit = (l2.val+carry)%10;
carry = (l2.val+carry)/10;
ListNode newNode = new ListNode(digit);
if(head==null)
head = newNode;
else
pre.next = newNode;
pre = newNode;
l2 = l2.next;
}
if(carry>0)
{
ListNode newNode = new ListNode(carry);
pre.next = newNode;
}
return head;
}
实现中注意维护进位,陷阱的话记住最后还要判一下有没有进位,如果有再生成一位。这道题还是有一些扩展的,比如这个其实是BigInteger的相加,数据结果不一定要用链表,也可以是数组,面试中可能两种都会问而且实现。然后接下来可以考一些OO设计的东西,比如说如果这是一个类应该怎么实现,也就是把数组或者链表作为成为成员变量,再把这些操作作为成员函数,进一步的问题可能是如何设计constructor,这个问题除了基本的还得对内置类型比如int,
long的constructor, 类似于BigInteger(int num), BigInteger(int long). 总体来说问题还是比较简单,但是这种问题不能出错,所以还是要谨慎对待。
分享到:
相关推荐
You are given two non-empty linked lists ... 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. java AC版本
leetcode 2 和 c 2021-LeetCode-02_Add_...addTwoNumbers(_ l1: ListNode?, _ l2: ListNode?) -> ListNode? { guard l1 != nil && l2 != nil else { return nil } var resultTail = ListNode() let resultHead = resu
python python_leetcode面试题解之两数相加AddTwoNumbers
leetcode Python 001 leetcode-问题-爬虫 ...002.add-two-numbers │ ├── information.json │ ├── README.md ... 有一些有用的选项: Options: -r, --rule crawling rule, eg1: 1-10, e
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. Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 ...
自己写的一个完整的程序,包括main函数,在VS上面提交通过,但是放到leetcode上面会出现问题;只是作为一个参考,一起学习学习0.o!解决的问题有:第一:两个链表的最后一个值相加后进位的问题;第二:两个链表的...
Add the two numbers and return the sum as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself. Example 1: Input: l1 = [2,4,3], l2 = [5,6,4] Output...
leetcode 接口 该项目可帮助您使用首选的 IDE 或带有命令行界面 (CLI) 的编辑器来执行 leetcode。 ...add-two-numbers leetcode --name median-of-two-sorted-arrays 然后问题描述和启动代码将自动
LeetCode-Web 初始化 前端库依赖 下载,并将jquery-3.x.x.min.js移动到static目录下。 下载,并将semantic.min.js、semantic.min.css、components和themes移动到static/semantic目录下。 下载,并将jquery.pjax.js...
leetcode:Add Two Numbers(java)
给你两个?非空 的链表,表示两个非负的整数。它们每位数字都是按照?逆序?的方式存储的,并且每个节点只能存储?一位?数字。请你将两个数相加,并以相同形式返回一个表示和的链表。你可以假设除了数字 0 之外,这两个...
AddTwoNumbers :简单的问题。 Anagrams :简单的#hashtable问题。 BalancedBinaryTree :简单的#balance #tree问题。 BestTimetoBuyandSellStock :简单的问题。 BestTimetoBuyandSellStockII :简单的问题。 ...
c代码-2. 两数相加 [中等] https://leetcode-cn.com/problems/add-two-numbers
swift代码-2. 两数相加 [中等] https://leetcode-cn.com/problems/add-two-numbers
java代码-2. 两数相加 [中等] https://leetcode-cn.com/problems/add-two-numbers
js代码-2. 两数相加 [中等] https://leetcode-cn.com/problems/add-two-numbers