Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Solution 1 - C++

Code Block
languagecpp
titletwonumbers.cpp
/**
 * Definition for singly-linked list.
 * struct ListNode {
 * int val;
 * ListNode *next;
 * ListNode(int x) : val(x), next(NULL) {}
 * };
 */

#define MSELECT(A,B) (A?A:(B?B:NULL))
#define MSELECTNEXT(A,B) (A?A->next:(B?B->next:NULL))

class Solution {
public:
 ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
 ListNode *n=new ListNode(0);
 if (l1) n->val+=l1->val;
 if (l2) n->val+=l2->val;
 if (n->val>9)
 {
 ListNode *next=MSELECTNEXT(l1,l2);
 if (next)
 {
 next->val += 1; 
 }
 else
 {
 ListNode *nc=new ListNode(1);
 ListNode *p=MSELECT(l1,l2);
 p->next=nc;
 }
 n->val -=10; 
 }
 if (MSELECT(l1,l2))
 n->next = addTwoNumbers((l1?l1->next:NULL), (l2?l2->next:NULL));
 else n->next =NULL;
 return n;
 }
};

https://leetcode.com/problems/add-two-numbers/description/

...