[LeetCode]2. Add Two Numbers链表相加

注意进位的处理和节点为null的处理

public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int flag = 0;
        ListNode res = null;
        ListNode temp = null;
        //注意如果进位不是0的话还要添加一个节点
        while (l1!=null||l2!=null||flag!=0)
        {
            int cur = flag;
            flag = 0;
            if (l1!=null) {
                cur+=l1.val;
                l1 = l1.next;
            }
            if (l2!=null) {
                cur+=l2.val;
                l2 = l2.next;
            }
            if (cur>9)
            {
                flag = 1;
                cur%=10;
            }
            if (res==null)
            {
                res = new ListNode(cur);
                temp = res;
                continue;
            }
            temp.next = new ListNode(cur);
            temp = temp.next;
        }
        return res;
    }

原文地址:https://www.cnblogs.com/stAr-1/p/8436632.html

时间: 2024-10-29 02:38:18

[LeetCode]2. Add Two Numbers链表相加的相关文章

leetcode 2 Add Two Numbers(链表)

数字反过来这个没有什么麻烦,就是镜像的去算十进制加法就可以了,然后就是简单的链表. /** * 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, ListN

LeetCode:Add Two Numbers - 两个链表逐项做带进位的加法生成新链表

1.题目名称 Add Two Numbers (两个链表逐项做带进位的加法生成新链表) 2.题目地址 https://leetcode.com/problems/add-two-numbers/ 3.题目内容 英文: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

LeetCode --- 2. Add Two Numbers

题目链接: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

【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 -&g

leetcode -day20 Add Two Numbers

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

【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 OJ - 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 -&

[C++]LeetCode: 108 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 -&