找LeetCode上简单的题目来撑过30天啦(DAY14)

医生说我很健康/images/emoticon/emoticon07.gif真是太好了呢,今日题目如下

**题号: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.

Example 1:

Input: l1 = [2,4,3], l2 = [5,6,4]
Output: [7,0,8]
Explanation: 342 + 465 = 807.

Example 2:
Input: l1 = [0], l2 = [0]
Output: [0]

Example 3:
Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
Output: [8,9,9,9,0,0,0,1]

Constraints:
• The number of nodes in each linked list is in the range [1, 100].
• 0 <= Node.val <= 9
• It is guaranteed that the list represents a number that does not have leading zeros.

我的程序码

/**
 * 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心得
我还得念书,劝说自己一点躺平/images/emoticon/emoticon02.gif


<<:  Day18 - CheckBox

>>:  [Day14] Storybook - Colors & Typography

Day 14: Draft

GOOGLE公云使用案例 大纲 Introduction(Global view) How to c...

#20 JS: Object Fundamentals

What is an Object? Introduction by W3C School Elem...

【Day33】[演算法]-深度优先搜寻DFS与广度优先搜寻BFS

深度优先搜寻(Depth-First Search,DFS)与广度优先搜寻(Breadth-Firs...

【不是铁人赛】Day 01|虚拟货币价格预测(一)资料处理

友:你要不要一起参加铁人赛? 我:好啊! (几天後) 我:乾我不小心忘了报名...... ----...

专业必备技能:应用程序相关

专业必备技能:应用程序相关 https://wolkesau.medium.com/专业必备技能-应...