LeetCode - Add Two Numbers

2013.12.1 21:03

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

Solution:

  Given two number represented as single-linked list, add them up and return the sum as a list in similar form.

  It seems easy enough, but you have to take care of different cases, mainly the following:

    1. NULL list

    2. the lengths of two lists are different

    3. 999 + 111 = 1110, the sum list needs one more digit

  Add up both 'numbers' one digit at a time, and record the carry for each digit. Make sure you write your code carefully, especially because this is linked list, the tricky thing.

  Time complexity is O(max(m, n)) where m and n are lengths of both linked lists. Space complexity is O(1) with some extra variables.

Accepted code:

  1 // 2WA, 1RE, 1AC, not so easy..
  2 /**
  3  * Definition for singly-linked list.
  4  * struct ListNode {
  5  *     int val;
  6  *     ListNode *next;
  7  *     ListNode(int x) : val(x), next(NULL) {}
  8  * };
  9  */
 10 class Solution {
 11 public:
 12     ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
 13         // IMPORTANT: Please reset any member data you declared, as
 14         // the same Solution instance will be reused for each test case.
 15         
 16         if(l1 == nullptr || l2 == nullptr){
 17             return nullptr;
 18         }
 19         
 20         ListNode *head;
 21         ListNode *ptr, *p1, *p2;
 22         bool first;
 23         ListNode *par1, *par2, *par;
 24         ListNode *r1, *r2, *rr;
 25         int carry;
 26         
 27         p1 = l1;
 28         p2 = l2;
 29         
 30         carry = 0;
 31         
 32         r1 = par1 = new ListNode(0);
 33         r2 = par2 = new ListNode(0);
 34         rr = par = new ListNode(0);
 35         
 36         par1->next = l1;
 37         par2->next = l2;
 38         par->next = nullptr;
 39         
 40         first = true;
 41         //while(p1 != nullptr && p2 != nullptr){
 42         // 1WA here, {1,8}, {0}
 43         while(p1 != nullptr || p2 != nullptr){
 44             ptr = new ListNode(0);
 45             if(p1 != nullptr){
 46                 ptr->val += p1->val;
 47             }
 48             if(p2 != nullptr){
 49                 ptr->val += p2->val;
 50             }
 51             /*
 52             1WA here, wrong if statement
 53             if(p1 != nullptr){
 54                 ptr->val += carry;
 55             }
 56             */
 57             ptr->val += carry;
 58 
 59             par->next = ptr;
 60             par = par->next;
 61             if(p1 != nullptr){
 62                 par1 = par1->next;
 63             }
 64             if(p2 != nullptr){
 65                 par2 = par2->next;
 66             }
 67 
 68             if(first){
 69                 head = ptr;
 70                 first = false;
 71             }
 72             
 73             if(ptr->val >= 10){
 74                 ptr->val -= 10;
 75                 carry = 1;
 76             }else{
 77                 carry = 0;
 78             }
 79             
 80             if(p1 != nullptr){
 81                 p1 = p1->next;
 82             }
 83             if(p2 != nullptr){
 84                 p2 = p2->next;
 85             }
 86             ptr = ptr->next;
 87         }
 88         if(carry){
 89             ptr = new ListNode(1);
 90             par->next = ptr;
 91         }
 92         
 93         // 1RE here, parent pointer moved, must have an extra pointer to record the parent node on the front
 94         delete r1;
 95         delete r2;
 96         delete rr;
 97         
 98         return head;
 99     }
100 };

 

 posted on 2013-12-01 21:26  zhuli19901106  阅读(216)  评论(0编辑  收藏  举报