趁着还有热情,接着刷。
题目:
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
题目意思大概就是把2个整数表示成倒叙的链表,然后加起来。
最开始写的时候以为java和C++似的,链表用->来表示下一个。
后来才注意到题目中给出了链表类的定义,果然改不了粗心的毛病。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
解题:1
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
if(l1 == null)
return l2;
if(l2 == null )
return l1;
ListNode result = null;//新链表的头结点
ListNode sum = null;//指向尾节点
int ge = 0;//因为只有2个一位数相加,所以涉及到进位,一个保存个位,一个保存十位
int shi = 0;
while(l1!=null||l2!=null)
{
ge = shi;//当前各位的刚才进的十位
if(l1!=null)
{
ge +=l1.val;
}
if(l2!=null)
{
ge +=l2.val;
}
shi = ge / 10;
ge = ge % 10;
if(sum == null)//链表为空,新节点为首节点
{
sum = new ListNode(ge);
result = sum;
}
else
{
sum.next = new ListNode(ge);
sum = sum.next;
}
if(l1!=null)
l1 = l1.next;
if(l2!=null)
l2 = l2.next;
}
if(shi !=0)//如果还有进位
sum.next = new ListNode(shi);
return result;
}
大致思路就是每次去当前节点和上一次的进位加起来,然后有进位的话就进,没有的话就直接作为新节点放在result里面。
代码:2
public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
if(l1 == null)
return l2;
if(l2 == null )
return l1;
ListNode result = null;//新链表的头结点
ListNode sum = null;//指向尾节点
int ge = 0;//因为只有2个一位数相加,所以涉及到进位,一个保存个位,一个保存十位
int shi = 0;
while(l1!=null&&l2!=null)
{
ge = shi;//当前各位的刚才进的十位
ge +=l1.val;
ge+=l2.val;
shi = ge / 10;
ge = ge % 10;
if(sum == null)//链表为空,新节点为首节点
{
sum = new ListNode(ge);
result = sum;
}
else
{
sum.next = new ListNode(ge);
sum = sum.next;
}
l1 = l1.next;
l2 = l2.next;
}
if(l1==null)
{
while(l2!=null){
ge = shi + l2.val;
shi = ge /10;
ge = ge % 10;
sum.next = new ListNode(ge);
sum = sum.next;
l2 = l2.next;
}
}
else if(l2==null)
{
while(l1!=null)
{
ge = shi + l1.val;
shi = ge /10;
ge = ge % 10;
sum.next = new ListNode(ge);
sum = sum.next;
l1 = l1.next;
}
}
if(shi !=0)//如果还有进位
sum.next = new ListNode(shi);
return result;
}
public static void main(String[] args) {
ListNode l1 = new ListNode(1);
ListNode l2 = new ListNode(9);
l2.next = new ListNode(9);
ListNode result = addTwoNumbers(l1,l2);
}
感觉还有一种投机取巧的写法。我先试试。写好了的话,补上来。代码3:
public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
if(l1 == null)
return l1;
if(l2 == null )
return l2;
ListNode result = null;//新链表的头结点
ListNode sum = null;//指向尾节点
int count = 0;
int firstNum = 0;
int SecondNum = 0;
while(l1!=null)
{
firstNum += l1.val*Math.pow(10, count++);
l1 = l1.next;
}
count = 0;
while(l2!=null)
{
SecondNum += l2.val*Math.pow(10, count++);
l2 = l2.next;
}
int ResultNum = firstNum+SecondNum;
System.out.println(ResultNum);
if(sum == null)
{
sum = new ListNode(ResultNum%10);
ResultNum /=10;
result = sum;
}
while(ResultNum!=0)
{
sum.next = new ListNode(ResultNum%10);
ResultNum /= 10;
sum = sum.next;
}
return result;
}
无奈会越界, 所以pass了。
分享到:
相关推荐
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版本
自己写的一个完整的程序,包括main函数,在VS上面提交通过,但是放到leetcode上面会出现问题;只是作为一个参考,一起学习学习0.o!解决的问题有:第一:两个链表的最后一个值相加后进位的问题;第二:两个链表的...
python python_leetcode面试题解之两数相加AddTwoNumbers
leetcode:Add Two Numbers(java)
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
给你两个?非空 的链表,表示两个非负的整数。它们每位数字都是按照?逆序?的方式存储的,并且每个节点只能存储?一位?数字。请你将两个数相加,并以相同形式返回一个表示和的链表。你可以假设除了数字 0 之外,这两个...
手绘算法力扣 2 两数相加(Add Two Numbers)
leetcode leetcode练习 twosum 问题 ;add two numbers问题;reverse integer问题;最大不重复子字符串长度问题;atoi问题;
leetcode 2 和 c 加二数解 LeetCode 两个数字相加的 C 语言解决方案 You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of ...
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2)...
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 ...
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 ...
2. Add Two Numbers 3. Longest Substring Without Repeating Characters 4. Median of Two Sorted Arrays 7. Reverse Integer 9. Palindrome Number 11. Container With Most Water 13. Roman to Integer 15. 3Sum ...
记录一下再LeetCode上刷的题,坚持每天刷一道吧 2017.06.12 打卡[LeetCode 2. Add Two Numbers], Linked list 2017.06.13 打卡[LeetCode 200. Number of Islands], BFS 2017.06.14 打卡[LeetCode 3. Longest ...
用C++刷Leetcode题 2. 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 ...
leetcode 2 Leetcode答案集 关于项目: 本项目包含本人LeetCode解题的答案,全部将由JavaScript语言进行解答。并会在每个题目的文件夹中添加相关的思路解析。 详情 # Title Solution Time Space Difficulty 1 Two ...
421 | [Maximum XOR of Two Numbers in an Array](https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/) | [C++](./C++/maximum-xor-of-two-numbers-in-an-array.cpp) [Python](./Python/...
格式为:0002_Add_Two_Numbers,先是题号,再跟上题目,题号与题目,以及题目单词间以下划线间隔; 题目做完之后,请在submittors目录下建立一个以微信昵称命名的文件,内容是你的commitID。这个步骤一周只需做一次...
leetcode 答案leetcode javascript 中 leetcode 测试的答案 twoSum addTwoNumbers
leetcode第321题 说明 每个代码里面都包含如下几部分: 自动生成测试数据的函数: random() 测试程序: main() 核心函数: 和LeetCode上面的模板函数同名 其他: 功能参见注解说明 注意:为了可以在测试程序里调用...