Add Two Numbers
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { if(l1==NULL) return l2; if(l2==NULL) return l1; ListNode *dummy=new ListNode(0); ListNode *cur; cur=dummy; int flag=0; while(l1!=NULL || l2!=NULL || flag){ int sum=(l1?l1->val:0)+(l2?l2->val:0)+flag; cur->next=new ListNode(sum); cur=cur->next; flag=sum/10; l1=l1?l1->next:l1; l2=l2?l2->next:l2; } return dummy->next; } };