Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- knakul853
- */
- class Solution {
- public:
- ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
- ListNode *p=l1;
- ListNode *q = l2;
- ListNode *ans = new ListNode(0);
- ListNode *curr = ans;
- int carry=0;
- while(p!=NULL || q!=NULL)
- {
- int n1 = p!=NULL?p->val:0;
- int n2 = q!=NULL?q->val:0;
- int sm = n1 + n2 + carry;
- carry=sm/10;
- curr->next = new ListNode(sm%10);
- curr = curr->next;
- if(p!=NULL)p=p->next;
- if(q!=NULL)q=q->next;
- }
- if(carry){
- curr->next=new ListNode(carry);
- curr=curr->next;
- }
- return ans->next;
- }
- };
Add Comment
Please, Sign In to add comment