Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Leetcode Add Two Numbers problem solution

YASH PAL, 31 July 202418 January 2026

In this Leetcode Add Two Numbers problem solution You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Leetcode Add Two Numbers problem solution

Leetcode Add Two Numbers problem solution in Python.

class Solution:
    def insert(self,l,v):
        if l==None:
            l.val=v
        else:
            r=l
            while r.next!=None:
                r=r.next
            tmp=ListNode(v)
            r.next=tmp
    def addTwoNumbers(self, l1, l2):
        res=ListNode(0)
        s,s2='',''
        while l1!=None:
            s+=str(l1.val)
            l1=l1.next
        while l2!=None:
            s2+=str(l2.val)
            l2=l2.next
        s=int(s[::-1])+int(s2[::-1])
        for x in str(s)[::-1]:
            self.insert(res,int(x))
        return res.next

Add Two Numbers problem solution in Java.

public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int carry = 0, sum;
        ListNode head = new ListNode(0);
        ListNode ln1 = l1, ln2 = l2, node = head;
        
        while (carry != 0 || ln1 != null || ln2 != null) {
            sum = carry; // reset sum to value of carry
            
            if (ln1 != null) {
                sum += ln1.val;
                ln1 = ln1.next;
            }
            if (ln2 != null) {
                sum += ln2.val;
                ln2 = ln2.next;
            }
            
            carry = sum / 10;
            node.next = new ListNode(sum % 10);
            node = node.next; 
        }
        return head.next;    
    }

Problem solution in C++.

class Solution {
public:
    
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        int val_1 , val_2, new_val, remainder;
        ListNode* not_null;
        ListNode* maybe_null;
        
        if(l1 == NULL && l2 == NULL){
            return NULL;
        }
        
        val_1 = (l1 == NULL? 0: l1->val);
        val_2 = (l2 == NULL? 0: l2->val);
        
        not_null = (l1 == NULL? l2: l1);
        maybe_null = ((l1 == not_null)? l2:l1);
        
        not_null->val= val_1+val_2;
        not_null->next = addTwoNumbers(not_null->next, (maybe_null == NULL?NULL:maybe_null->next));
        
        remainder = floor(not_null->val/10);
        not_null->val = (not_null->val%10);
        ListNode* this_node= not_null;
        
        while(remainder != 0 && this_node->next!=NULL){
            (this_node->next->val)+=remainder;
            remainder= floor(this_node->next->val/10);
            (this_node->next->val) = (this_node->next->val%10);
            this_node = this_node->next;
        }
        
        if(remainder!= 0 && this_node->next==NULL){
            this_node->next = new ListNode(remainder);
        }
        
        return not_null;
    }
};

Problem solution in C.

struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){
    struct ListNode *p = l1, *q = l2;
    struct ListNode *result = NULL, *curr = NULL;
    int carry = 0;
    while (p != NULL || q != NULL || carry != 0) {
        struct ListNode* newNode = (struct ListNode*)malloc(sizeof(struct ListNode));
        int x = (p != NULL) ? p->val : 0;
        int y = (q != NULL) ? q->val : 0;
        int sum = (carry + x + y) % 10;
        carry = (carry + x + y) / 10;
        newNode->val = sum;
        newNode->next = NULL;
        if (result == NULL) {
            result = curr = newNode;
        } else {
            curr->next = newNode;
            curr = newNode;
        }
        if (p != NULL) p = p->next;
        if (q != NULL) q = q->next;
    }
    return result;
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes