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

这一题主要是熟悉单链表的操作,链表操作使用一个pHead记录链表头位置,还需要使用一个不停向后移动的指针pMove来遍历链表中的元素。

我第一次的思路及代码如下:

 ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) 
    {
        int iCarry = 0;
        ListNode *pL1 = l1;
        ListNode *pL2 = l2;
        ListNode *pRes = NULL;
        ListNode *pMove = NULL;
        ListNode * pTemp = NULL;
        int flag = 1;

        while(pL1 != NULL && pL2 != NULL)
        {
            
            pTemp = new ListNode(sizeof(struct ListNode));
            if(flag == 1)
            {
                pMove = pRes = pTemp;
                ++flag;
            }
            else
            {
                pMove->next = pTemp;
                pMove = pTemp;
            }

            pTemp->val = pL1->val + pL2->val + iCarry;

            if(pTemp->val >= 10)
            {
                pTemp->val = pTemp->val % 10;
                iCarry = 1;
            }
            else
            {
                iCarry = 0;
            }

            pL1 = pL1->next;
            pL2 = pL2->next;
        }
        while(pL1 != NULL || pL2 != NULL)
        {
            if( pL2 != NULL )
            {
                pTemp =  new ListNode (sizeof(struct ListNode));
                pMove->next = pTemp;
                pMove = pTemp;

                pMove->val = pL2->val + iCarry;
                iCarry = 0;
                if(pMove->val >= 10)
                {
                    pMove->val %= 10;
                    iCarry = 1;
                    pL2= pL2->next;
                }
                else
                {
                    pL2= pL2->next;
                }
            }
            if(pL1 != NULL)
            {
                pTemp =  new ListNode (sizeof(struct ListNode));
                pMove->next = pTemp;
                pMove = pTemp;

                pMove->val = pL1->val + iCarry;
                iCarry = 0;
                if(pMove->val >= 10)
                {
                    pMove->val %= 10;
                    iCarry = 1;
                    pL1= pL1->next;
                }
                else
                {
                    pL1= pL1->next;
                }
            }
        }
        if(pL1 == NULL && pL2 == NULL && iCarry != 0)
        {
            pTemp =  new ListNode (sizeof(struct ListNode));
            pMove->next = pTemp;
            pMove = pTemp;
            pTemp->val = 1;
        }
        return pRes;
    }

真是一段又长又臭的代码,这差不多也就是我现在的水平,还亟待提高。逻辑关系没有理很清楚,代码没有做到最优。
仔细想,其实这里的加法,只需用四个变量iVal1,iVal2,iCarry,iRes来分别记录数据处理的过程,然后就是依次存储到链表中。

就是这段:

 if(flag == 1)
   {
    pMove = pRes = pTemp;
    ++flag;
   }
   else
   {
    pMove->next = pTemp;
    pMove = pTemp;
   }

后来参考网上代码,所有情况其实只需考虑l1 l2是否非空即可,如下。

ListNode *addTwoNumbers(ListNode *l1, ListNode *l2)
    {
        int iCarry = 0;
        int iFlag = 1;
        ListNode *pTemp = NULL;
        ListNode *pHead = NULL;
        ListNode *pMove = NULL;
        while(l1 != NULL || l2 != NULL)
        {
            int iVal1 = 0;
            if(l1 != NULL)
            {
                iVal1 = l1->val;
                l1 = l1->next;
            }
            int iVal2 = 0;
            if(l2 != NULL)
            {
                iVal2 = l2->val;
                l2 = l2->next;
            }
            int temp = iVal1 + iVal2 + iCarry;
            pTemp = new ListNode(sizeof(ListNode));
            if(iFlag == 1)
            {
                pHead = pMove = pTemp;
                ++iFlag;
            }
            else
            {
                pMove->next = pTemp;
                pMove = pTemp;
            }
            pTemp->val = temp % 10;
            iCarry = temp / 10;
        }
        if(iCarry == 1)
        {
            pTemp = new ListNode(sizeof(ListNode));
            pMove->next = pTemp;
            pMove = pTemp;
            pTemp->val = 1;
        }
        return pHead;
    }

逻辑关系明确,这才是最终的best code。相信自己最终也能达到这样的水平。

 

posted @ 2015-03-21 19:50  BestWangJie  阅读(124)  评论(0编辑  收藏  举报