Sunday 17 July 2016

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


 /**  
  * struct ListNode {  
  *   int val;  
  *   struct ListNode *next;  
  * };  
  */  
 struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {  
   if(l1 == NULL && l2 == NULL){  
     return NULL;  
   }  
   struct ListNode* newHead = NULL, *temp;  
   int sum, carry = 0;  
   while(l1 && l2){  
     sum = l1->val + l2->val + carry;  
     if(sum > 9){  
       sum = sum % 10;  
       carry = 1;  
     }else{  
       carry = 0;  
     }  
     struct ListNode *newNode = (struct ListNode*)malloc(sizeof(struct ListNode));  
     newNode->val = sum;  
     newNode->next = NULL;  
     if(newHead == NULL){  
       newHead = newNode;  
     }else{  
       temp = newHead;  
       while(temp->next){  
         temp = temp->next;  
       }  
       temp->next = newNode;  
     }  
     l1 = l1->next;  
     l2 = l2->next;  
   }  
   if(l1){  
     //if l1 is not NULL and there is no carry  
     if(!carry){  
       temp = newHead;  
       while(temp->next){  
         temp = temp->next;  
       }  
       temp->next = l1;  
     }else{  
       //if l1 is not NULL but there is a carry  
       while(l1){  
         sum = carry + l1->val;  
         if(sum > 9){  
           sum = sum % 10;  
           carry = 1;  
         }else{  
           carry = 0;  
         }  
         struct ListNode *newNode = (struct ListNode*)malloc(sizeof(struct ListNode));  
         newNode->val = sum;  
         newNode->next = NULL;  
         temp = newHead;  
         while(temp->next){  
           temp = temp->next;  
         }  
         temp->next = newNode;  
         l1 = l1->next;  
       }  
     }  
   }  
   if(l2){  
     //if l2 is not NULL and there is no carry  
     if(!carry){  
       temp = newHead;  
       while(temp->next){  
         temp = temp->next;  
       }  
       temp->next = l2;  
     }else{  
       //if l2 is not NULL but there is a carry  
       while(l2){  
         sum = carry + l2->val;  
         if(sum > 9){  
           sum = sum % 10;  
           carry = 1;  
         }else{  
           carry = 0;  
         }  
         struct ListNode *newNode = (struct ListNode*)malloc(sizeof(struct ListNode));  
         newNode->val = sum;  
         newNode->next = NULL;  
         temp = newHead;  
         while(temp->next){  
           temp = temp->next;  
         }  
         temp->next = newNode;  
         l2 = l2->next;  
       }  
     }  
   }  
   //if l1 and l2 are NULL but there is a carry  
   if(!l1 && !l2 && carry){  
     struct ListNode *newNode = (struct ListNode*)malloc(sizeof(struct ListNode));  
     newNode->val = 1;  
     newNode->next = NULL;  
     temp = newHead;  
     while(temp->next){  
       temp = temp->next;  
     }  
     temp->next = newNode;  
   }  
   return newHead;  
 }  
Share:

0 comments:

Post a Comment

Contact Me

Name

Email *

Message *

Popular Posts

Blog Archive