LeetCode 2_Add Two Numbers

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

Output: 7 -> 0 -> 8

题目很清晰(虽然我看了半天),无非链表相加!

注意:

1、链表长度没说一样长。

2、考虑最后一个节点有可能进位,此时需要新建节点。

首先不加思索写出的代码:

 ListNode* addTwoNumbers(ListNode* l1, ListNode* l2)
    {
        ListNode* l3 = l1;
        int tmp1 = 0, tmp = 0;
        while(l1->next != NULL && l2->next != NULL)
        {
            tmp1 = l1->val;
            l1->val = (l1->val + l2->val + tmp) % 10;
            tmp = (tmp1 + l2->val + tmp) / 10;
            l1 = l1->next;
            l2 = l2->next;
        }
        if(l1->next == NULL && l2->next == NULL)
        {
            tmp1 = l1->val;
            l1->val = (l1->val + l2->val + tmp) % 10;
            tmp = (tmp1 + l2->val + tmp) / 10;
            if(tmp == 1)
                l1->next = new ListNode(1);
            return l3;
        }
        if(l1->next != NULL)
        {
            tmp1 = l1->val;
            l1->val = (l1->val + l2->val + tmp) % 10;
            tmp = (tmp1 + l2->val + tmp) / 10;
            l1 = l1->next;
            while(l1->next != NULL)
            {
                if(l1->val + tmp < 10)
                {
                    l1->val = l1->val + tmp;
                    return l3;
                }
                else
                {
                    l1->val = (l1->val + tmp) % 10;
                    tmp = 1;
                    l1 = l1->next;
                }
            }
            if(l1->val + tmp >= 10)
            {
                l1->val = (l1->val + tmp) % 10;
                l1->next = new ListNode(1);
                return l3;
            }
            else
            {
                l1->val = (l1->val + tmp) % 10;
                return l3;
            }
        }
        if(l2->next != NULL)
        {
            tmp1 = l1->val;
            l1->val = (l1->val + l2->val + tmp) % 10;
            tmp = (tmp1 + l2->val + tmp) / 10;
            l1->next = l2->next;
            l1 = l1->next;
            while(l1->next != NULL)
            {
                if(l1->val + tmp < 10)
                {
                    l1->val = l1->val + tmp;
                    return l3;
                }
                else
                {
                    l1->val = (l1->val + tmp) % 10;
                    tmp = 1;
                    l1 = l1->next;
                }
            }
            if(l1->val + tmp >= 10)
            {
                l1->val = (l1->val + tmp) % 10;
                l1->next = new ListNode(1);
                return l3;
            }
            else
            {
                l1->val = (l1->val + tmp) % 10;
                return l3;
            }

        }
    }

这代码,看起来好low,首先代码重复性太大,从代码的编写就可以有大量的优化空间。

最终优化为(其实也没什么优化,只是删掉了一些重复):

 ListNode* addTwoNumbers(ListNode* l1, ListNode* l2)
    {
        ListNode* l3 = l1;
        int tmp1 = 0, tmp = 0;
        while(l1->next != NULL && l2->next != NULL)
        {
            tmp1 = l1->val;
            l1->val = (l1->val + l2->val + tmp) % 10;
            tmp = (tmp1 + l2->val + tmp) / 10;
            l1 = l1->next;
            l2 = l2->next;
        }
        tmp1 = l1->val;
        l1->val = (l1->val + l2->val + tmp) % 10;
        tmp = (tmp1 + l2->val + tmp) / 10;
        if(l1->next == NULL && l2->next == NULL)
        {
            if(tmp == 1)
                l1->next = new ListNode(1);
            return l3;
        }
        else
        {
            if(l2->next != NULL)
                l1->next = l2->next;
            l1 = l1->next;
            while(l1->next != NULL)
            {
                if(l1->val + tmp < 10)
                {
                    l1->val = l1->val + tmp;
                    return l3;
                }
                else
                {
                    l1->val = (l1->val + tmp) % 10;
                    tmp = 1;
                    l1 = l1->next;
                }
            }
            if(l1->val + tmp >= 10)
            {
                l1->val = (l1->val + tmp) % 10;
                l1->next = new ListNode(1);
                return l3;
            }
            else
            {
                l1->val = (l1->val + tmp) % 10;
                return l3;
            }
        }</span>

其实有一种更简单的方法是再新建一个链表,不过就是会浪费空间

 ListNode* addTwoNumbers(ListNode* l1, ListNode* l2)
    {
        int addNum = 0;
        ListNode *l3 = new ListNode(0);
        ListNode *ptr = l3;
        while(l1 != NULL || l2 != NULL)
        {
            int val1 = 0;
            if(l1 != NULL)
            {
                val1 = l1->val;
                l1 = l1->next;
            }  

            int val2 = 0;
            if(l2 != NULL)
            {
                val2 = l2->val;
                l2 = l2->next;
            }  

            ListNode *temp = new ListNode((val1 + val2 + addNum) % 10);
            ptr->next = temp;
            ptr = temp;
            addNum = (val1 + val2 + addNum) / 10;
        }
        if(addNum == 1)
           ptr->next = new ListNode(1);
        return l3->next;
   }

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-18 10:58:58

LeetCode 2_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

题目:两个链表存储数字,然后求和,和值存储在一个链表中. 代码: 1 public ListNode addTwoNumbers(ListNode l1, ListNode l2) { 2 ListNode head = new ListNode(0); 3 ListNode result = head; 4 5 int carry = 0 , tempSum = 0; 6 while(l1 != null || l2 != null){ 7 int v1 , v2; 8 v1 = (l1 !=

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 两个链表表示的正整数对其求和(AC)

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

2_Add Two Numbers --LeetCode

原题如下: 思路:在一个while中遍历两个链表,直到最长的链表为空,或者没有进位.每一步获取两个链表对应的结点的值a,b,然后相加a+b.如果上一步又进位,那就加a+b+1,若由于进位加1后还产生进位,则设置进位标识位为true.如果a+b大于9,也要设置进位标识为true. 代码如下: 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 *

LeetCode:Consecutive Numbers - 找出连续出现的数字

1.题目名称 Consecutive Numbers(找出连续出现的数字) 2.题目地址 https://leetcode.com/problems/consecutive-numbers/ 3.题目内容 写一个SQL,查出表Logs中连续出现至少3次的数字: +----+-----+ | Id | Num | +----+-----+ | 1  |  1  | | 2  |  1  | | 3  |  1  | | 4  |  2  | | 5  |  1  | | 6  |  2  | | 

[LeetCode] Valid Phone Numbers 验证电话号码

Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bash script to print all valid phone numbers. You may assume that a valid phone number must appear in one of the following two formats: (xxx) xxx-xxxx or

【Leetcode】Count Numbers with Unique Digits

题目链接:https://leetcode.com/problems/count-numbers-with-unique-digits/ 题目: Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Example: Given n = 2, return 91. (The answer should be the total numbers in the range

LeetCode: Add Two Numbers 解题报告

Add Two NumbersYou 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