• 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


    此题着实不难,然考虑甚多. 边界情况种种, 不一而足, 耗时亦两小时, 实感愧怍.

    委屈

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
      /*  ListNode *reverseList(ListNode *head)
        {
            if(!head||!head->next)
                return head;
            ListNode *p,*q,*r;
            p = NULL;
            q = head;
            r = head->next;
            while(r)
            {
                q->next = p;
                p = q;
                q = r;
                r = r->next;
            }
            return q;
        }
        */
        ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) 
        {
            ListNode *p1, *p2;
            //p1 = reverseList(l1);
            //p2 = reverseList(l2);
            p1 = l1;
            p2 = l2;
            if(!p1) return p2;
            if(!p2) return p1;
            if(!p1->next&&!p2->next)
            {
                if(p1->val+p2->val<10)
                {
                    p1->val += p2->val;
                    return p1;
                }
                else
                {
                    p1->val = (p1->val+p2->val)%10;
                    p2->val = 1;
                    p1->next = p2;
                    return p1;
                }
            }
            int carry = 0;
            ListNode *p = NULL;
            ListNode *newHead = NULL;
            ListNode *pNext = NULL;
            while(p1&&p2)
            {
               pNext = new ListNode(0);
               if(!p)
                {
                    p = pNext;
                    newHead = p;
                }
                else
                {
                    p->next  = pNext;
                    p = pNext;
                }
                   
                pNext->val = (p1->val+p2->val+carry)%10;
                carry  = (p1->val+p2->val+carry)/10;
                if(!p1->next&&!p2->next) //{5},{5}
                {
                    if(carry == 1)
                    {
                        pNext = new ListNode(1);
                        p->next  = pNext;
                        return newHead;
                    }
                }
                    p1 = p1->next;
                    p2 = p2->next;
                }
    
            if(!p1)
            {
                while(p2)
                {
                    pNext = new ListNode(0);
                    pNext->val = (p2->val+carry)%10;
                    carry  = (p2->val+carry)/10;
                    p2 = p2->next;
                    p->next  = pNext;
                    p = pNext;
                }
                if(carry == 1)
                {
                     pNext = new ListNode(1);
                     p->next = pNext;
                }
            }
            if(!p2)
            {
                while(p1)
                {
                    pNext = new ListNode(0);
                    pNext->val = (p1->val+carry)%10;
                    carry  = (p1->val+carry)/10;
                    p1 = p1->next;
                    p->next  = pNext;
                    p = pNext;
                }
                 if(carry == 1)
                {
                     pNext = new ListNode(1);
                     p->next = pNext;
                }
            }
            return newHead;
            
        }
    };


    每天早上叫醒你的不是闹钟,而是心中的梦~
  • 相关阅读:
    python 基础2
    ffmpeg安装和录制linux桌面图像
    Python TCP Socket 传输服务器资源信息(C/S)
    ubuntu下,hue3.7编译安装,设置中文语言
    Python 图片转字符画
    Python快速教程
    spark安装部署
    python基础之文件处理
    python之路之函数
    python习题
  • 原文地址:https://www.cnblogs.com/vintion/p/4116998.html
Copyright © 2020-2023  润新知