Very ease c++ solution by making two list size equal

int len(ListNode* t){
int p=0;
while(t){
p++;
t=t->next;
}
return p;
}
ListNode* Solution::addTwoNumbers(ListNode* p, ListNode* q) {
if(!p)return q;
if(!q)return p;
int p_len=len§;
int q_len=len(q);
if(p_len>q_len){
ListNode* tt=q;
while(tt->next){
tt=tt->next;
}
int diff=p_len-q_len;
for(int i=0;i<diff;i++){
tt->next=new ListNode(0);
tt=tt->next;
}
}
else if(p_len<q_len){
ListNode* tt=p;
while(tt->next){
tt=tt->next;
}
int diff=q_len-p_len;
for(int i=0;i<diff;i++){
tt->next=new ListNode(0);
tt=tt->next;
}
}
ListNode* dummy= new ListNode(-9999);
ListNode *curr=dummy;
int carry=0;
int sum=p->val+q->val;
curr->next=new ListNode(sum%10);
carry=sum/10;
curr=curr->next;
p=p->next;
q=q->next;
while§{
if(carry){
int sum=p->val+q->val+1;
curr->next=new ListNode(sum%10);
carry=sum/10;
curr=curr->next;
p=p->next;
q=q->next;
}
else if(!carry){

   int sum=p->val+q->val;

curr->next=new ListNode(sum%10);
carry=sum/10;
curr=curr->next;
p=p->next;
q=q->next;
}
if(carry){
curr->next=new ListNode(1);
}
}
return dummy->next;
}

Click here to start solving coding interview questions