【Leetcode】2. Add Two Numbers

Problem:

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 do not contain any leading zero, except the number 0 itself.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)

Output: 7 -> 0 -> 8

Resolution

public ListNode addTwoNumbers(ListNode l1,ListNode l2){
    int carry =0;//每一位的数值
    ListNode p,result = new ListNode(0);
    p=result;
    while(l1!=null||l2!=null||carry!=0){
        if(l1!=null){
            carry+=l1.val;
            l1=l1.next;
        }
        if (l2!=null){
           carry+=l2.val;
           l2=l2.next;
        }
        p.next=new ListNode(carry%10);
        carry/10;
        p=p.next;
    }
    return result.next;
}

注:

①需要保证l1或l2非空。在carry!=0时也要继续进行计算的原因是当l1与l2均进行完加法,到达链表末尾,不会出现溢出问题。

②处理方式并非相对l1与l2的val加和在赋值给carry,而是carry+=l1.val,在carry+=l2.val;

时间: 2024-12-13 07:29:29

【Leetcode】2. Add Two Numbers的相关文章

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

【Leetcode】445. Add Two Numbers II

You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first 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 两数相加

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】:Compare Version Numbers

题目地址:https://oj.leetcode.com/problems/compare-version-numbers/ 题目描述: Compare two version numbers version1 and version1. If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. You may assume that the version strings ar

【Leetcode】Bitwise AND of Numbers Range

题目链接:https://leetcode.com/problems/bitwise-and-of-numbers-range/ 题目: Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive. For example, given the range [5, 7], you should return 4. 思

【Leetcode】Count of Smaller Numbers After Self

题目链接:https://leetcode.com/problems/count-of-smaller-numbers-after-self/ 题目: You are given an integer array nums and you have to return a new counts array. The counts array has the property where counts[i] is the number of smaller elements to the righ

【LeetCode】258 - Add Digits

Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. For example: Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it. Follow up:Could you do it without any

【LeetCode】165 - Compare Version Numbers

Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. You may assume that the version strings are non-empty and contain only digits and the . character.The . characte

【leetcode】Bitwise AND of Numbers Range(middle)

Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive. For example, given the range [5, 7], you should return 4. 思路: 先找前面二进制相同的位,后面不相同的位相与一定为0. 比如: 1111001 1110011 从0011 - 1001 中间一定会经