zoukankan      html  css  js  c++  java
  • [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

    Solutions:

    /**
     * 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) {
            ListNode *ans = NULL, *end = NULL, *num1 = l1, *num2 = l2;
            int carry = 0, sum = 0;
            while(true)
            {
                if(num1 != NULL)
                {
                    if(num2 != NULL)
                    {
                        sum = num1 -> val + num2 -> val + carry;
                        carry = sum / 10;
                        if(ans == NULL)
                        {
                            ans = new ListNode(sum % 10);
                            end = ans;
                        }
                        else
                        {
                            end -> next = new ListNode(sum % 10);
                            end = end -> next;
                        }
                        num1 = num1 -> next;
                        num2 = num2 -> next;
                    }
                    else
                    {
                        sum = num1 -> val + carry;
                        carry = sum / 10;
                        if(ans == NULL)
                        {
                            ans = new ListNode(sum % 10);
                            end = ans;
                        }
                        else
                        {
                            end -> next = new ListNode(sum % 10);
                            end = end -> next;
                        }
                        num1 = num1 -> next;
                    }
                }
                else
                {
                    if(num2 != NULL)
                    {
                        sum = num2 -> val + carry;
                        carry = sum / 10;
                        if(ans == NULL)
                        {
                            ans = new ListNode(sum % 10);
                            end = ans;
                        }
                        else
                        {
                            end -> next = new ListNode(sum % 10);
                            end = end -> next;
                        }
                        num2 = num2 -> next;
                    }
                    else
                    {
                        if(carry == 1)
                        {
                            end -> next = new ListNode(1);
                            end = end -> next;
                        }
                        break;
                    }
                }
            }
            
            return ans;
        }
    };
  • 相关阅读:
    【NOIP2017】蚯蚓
    【CF407B】Long Path
    【NOIP2017】奶酪
    【NOIP2018】赛道修建(正解)
    【NOIP2018】旅行
    【SDOI2010】地精部落
    【NOIP2017】逛公园
    百度云网盘进行注销操作
    百度超级会员租借.我租给你。
    如何在dos中运行java中的jar包
  • 原文地址:https://www.cnblogs.com/changchengxiao/p/3594790.html
Copyright © 2011-2022 走看看