LeetCode 2 Add Two Numbers

翻译:

给你两个表示两个非负数字的链表。数字以相反的顺序存储,其节点包含单个数字。将这两个数字相加并将其作为一个链表返回。

输入: (2 -> 4 -> 3) + (5 -> 6 -> 4)
输出: 7 -> 0 -> 8

原题:

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

C++

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        int carry=0;
        ListNode* listNode=new ListNode(0);
        ListNode* p1=l1,*p2=l2,*p3=listNode;

        while(p1!=NULL|p2!=NULL)
        {
            if(p1!=NULL)
            {
                carry+=p1->val;
                p1=p1->next;
            }
            if(p2!=NULL)
            {
                carry+=p2->val;
                p2=p2->next;
            }
            p3->next=new ListNode(carry%10);
            p3=p3->next;
            carry/=10;
        }
        if(carry==1)
            p3->next=new ListNode(1);
        return listNode->next;
    }
};

C#

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public int val;
 *     public ListNode next;
 *     public ListNode(int x) { val = x; }
 * }
 */
public class Solution
{
    public ListNode AddTwoNumbers(ListNode l1, ListNode l2)
    {
        int carry = 0;
        ListNode listNode= new ListNode(0);
        ListNode p1 = l1, p2 = l2, p3 = listNode;

        while (p1 != null || p2 != null)
        {
            if (p1 != null)
            {
                carry += p1.val;
                p1 = p1.next;
            }
            if (p2 != null)
            {
                carry += p2.val;
                p2 = p2.next;
            }
            p3.next = new ListNode(carry % 10);
            p3 = p3.next;
            carry /= 10;
        }
        if (carry == 1)
            p3.next = new ListNode(1);
        return listNode.next;
    }
}

Java

public class Solution {
     public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
         int carry=0;
         ListNode listNode=new ListNode(0);
         ListNode p1=l1,p2=l2,p3=listNode;
         while(p1!=null||p2!=null){
             if(p1!=null){
                 carry+=p1.val;
                 p1=p1.next;
             }
             if(p2!=null){
                 carry+=p2.val;
                 p2=p2.next;
             }
             p3.next=new ListNode(carry%10);
             p3=p3.next;
             carry/=10;
         }
         if(carry==1)
             p3.next=new ListNode(1);
         return listNode.next;
     }
}

C++(来源于网络)

class Solution {
public:
    ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
        int carry=0;
        ListNode* res=new ListNode(0);
        ListNode* head = res;
        while (l1 && l2){
            res->next=new ListNode((l1->val+l2->val+carry)%10);
            carry = (l1->val+l2->val+carry)/10;
            l1=l1->next;
            l2=l2->next;
            res=res->next;
        }
        while (l1){
            res->next=new ListNode((l1->val+carry)%10);
            carry = (l1->val+carry)/10;
            l1=l1->next;
            res=res->next;
        }
        while (l2){
            res->next=new ListNode((l2->val+carry)%10);
            carry = (l2->val+carry)/10;
            l2=l2->next;
            res=res->next;
        }
        if (carry>0){
            res->next = new ListNode(carry);
        }
        return head->next;
    }
};
时间: 2024-12-27 17:11:00

LeetCode 2 Add Two Numbers的相关文章

leetcode 002 add two numbers C语言

问题描述 leetcode 002 add two numbers C语言 struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) { struct ListNode *l, *p; l = (struct ListNode*)malloc(sizeof(struct ListNode)); l->val = l1->val + l2->val; p = l; l1 = l1->next; l

求帮助:2. Add Two Numbers ,已疯,谢谢

问题描述 求帮助:2. Add Two Numbers ,已疯,谢谢 自己运行: 提交以后: 代码: /** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) { struct ListNode *l,

[LeetCode] Sum of Square Numbers 平方数之和

Given a non-negative integer c, your task is to decide whether there're two integers a and b such that a2 + b2 = c. Example 1: Input: 5 Output: True Explanation: 1 * 1 + 2 * 2 = 5  Example 2: Input: 3 Output: False 这道题让我们求一个数是否能由平方数之和组成,刚开始博主没仔细看题,没有

[LeetCode] Add Two Numbers

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

[LeetCode]165.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 are non-empty and contain only digits and the . character. The . c

LeetCode 258 Add Digits(数字相加,数字根)

翻译 给定一个非负整型数字,重复相加其所有的数字直到最后的结果只有一位数. 例如: 给定sum = 38,这个过程就像是:3 + 8 = 11,1 + 1 = 2,因为2只有一位数,所以返回它. 紧接着: 你可以不用循环或递归在O(1)时间内完成它吗? 原文 Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. For example: Given

leetcode 67 Add Binary

Add Binary Total Accepted: 46815 Total Submissions: 189215 My Submissions                       Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 我的解决方案: class Solu

Add Two Numbers

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

[LeetCode] 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. 解题思路 ① n&(n-1),可以去除n的最低位的1. ② 从n一直与到m,可以去掉的1就是n和m的右端不相等的部分的1. 例如对于