【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 -> 4)

Output: 7 -> 0 -> 8

class Solution {
public:
    ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
        int carry = 0;
        ListNode *l3 = new ListNode(0);
        ListNode *p3 = l3;
        while(l1 !=NULL || l2!= NULL){
            if(l1 != NULL){
                carry += l1->val;
                l1 = l1->next;
            }
            if(l2 != NULL){
                carry += l2->val;
                l2 = l2->next;
            }

            l3->next = new ListNode(carry % 10);
            carry = carry/10;
            l3 = l3->next;
        }
        if(carry == 1)
            l3->next = new ListNode(1);
        return p3->next;
    }
};
时间: 2024-10-15 13:23:26

【leetcode】 Add Two Numbers的相关文章

【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】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】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】Add Two Numbers(middle) ☆

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】Add Two Numbers(两数相加)

这道题是LeetCode里的第2到题. 这道题的条件判断很简单,如下: 1.是否为尾节点 2.是否产生进位 3.是否等于9 4.是否需要拓展空间 代码如下: ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { ListNode *p = l1, *q = l2; int add, carry = 0;//carry标志进位 while (1) { add = p->val + q->val + carry; p->val = add

【LeetCode】Add Binary

Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". public class Solution { public String addBinary(String a, String b) { if(a.equalsIgnoreCase("")||a==null) r

LeetCode【2】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 ->

【链表】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】Add Digits

题目链接:https://leetcode.com/problems/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 digi