医生说我很健康真是太好了呢,今日题目如下
**题号:2 标题:Add Two Numbers 难度:Medium
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 contains a single digit. 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.
我的程序码
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
int count = 0;
int a,b;
ListNode result = new ListNode();
ListNode result2 = new ListNode();
ListNode head = result;
ListNode tail = result;
int c = 0;
while(l1 != null || l2 != null){
ListNode temp = new ListNode();
if(l1 != null && l2 != null){
a=l1.val; b=l2.val;
}else if(l1 == null){
a=0;b=l2.val;
}else{
a=l1.val; b=0;
}
System.out.println(a +"," +b+","+count);
temp.val = (a+b+count)%10;
//System.out.println(temp.val);
if(head==null){
head = temp;
tail = temp;
System.out.println(temp.val);
}else{
tail.next = temp;
tail =temp;
}
if((a+b+count)/10 > 0){
count = 1;
}else{
count = 0;
}
if(l1 !=null){
l1 = l1.next;
}
if(l2 !=null){
l2 = l2.next;
}
if(l2 ==null && l1 ==null && count ==1){
ListNode temp2 = new ListNode();
temp2.val = 1;
tail.next = temp2;
tail =temp2;
}
}
result2 = result.next;
return result2;
}
}
花比较久的时间在
linklist上网查了一下怎麽用,虽然修修改改是把这题写完了,之後有时间要来好好了解linklist怎麽用阿
DAY14心得
我还得念书,劝说自己一点躺平
>>: [Day14] Storybook - Colors & Typography
GOOGLE公云使用案例 大纲 Introduction(Global view) How to c...
What is an Object? Introduction by W3C School Elem...
深度优先搜寻(Depth-First Search,DFS)与广度优先搜寻(Breadth-Firs...
友:你要不要一起参加铁人赛? 我:好啊! (几天後) 我:乾我不小心忘了报名...... ----...
专业必备技能:应用程序相关 https://wolkesau.medium.com/专业必备技能-应...