Add Two Numbers

来源:互联网 发布:java api 1.8中文 编辑:程序博客网 时间:2024/06/04 08:58

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


0 0
原创粉丝点击