Add Two Numbers in Linked List
Medium Problems of Singly Linked List DSA practice problem on Onlearn.
Difficulty: medium.
Topics: Adding Two Numbers Represented by Linked Lists (Digits Stored in Reverse Order), Linked List, Node, Pointer, Singly Linked List, Algorithm, Time Complexity, Space Complexity, Basic Arithmetic, Loops, Conditional Statements, mathematical operations, edge cases, linked list, Linked List Addition.
Problem Statement : Given the heads of two non empty singly linked lists, l1 and l2, 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. Input Specification : The input consists of pointers/access to the heads of the two linked lists. Output Specification : Return the head of a new singly linked list representing the sum of the two numbers. The digits in the sum list should also be stored in reverse order. Constraints : The linked lists are non empty. The digits are stored in reverse order. Each node contains a single digit. Sample Test Cases : Sample 1: Input: l1 = [2,4,3] (Represents number 342) l2 = [5,6,4] (Represents number 465) Output: [7,0,8] (Represents number 807) Explanation: Since the digits are stored in reverse order, reverse the numbers first to get the original number and then add them as: 342 + 465 = 807. Sample 2: Input: l1 = [9,9,9,9,9,9,9] (Represents number 9999999) l2 = [9,9,9,9] (Represents number 9999) Output: [8,9,9,9,0,0,0,1] (Represents number 10009998) Explanation: Since the digits are stored in reverse order, reverse the numbers first to get the original number and then add them as: 9999999 + 9999 = 10009998.