Add two Numbers Represented by Linked Lists in C++
The below code is an implementation of adding two numbers represented as linked lists in C++. In this implementation, the input linked lists are assumed to represent the numbers in reverse order, i.e., the least significant digit is at the head of the list. The implementation starts by defining a structure for the nodes of the linked list. Each node has an integer value and a pointer to the next node in the list. The implementation also defines a function called addTwoNumbers that takes two pointers to the heads of the input linked lists as arguments and returns a pointer to the head of the resulting linked list.
7 0 8 The result id due to formation: Input: l1: 2 -> 4 -> 3 l2: 5 -> 6 -> 4 Internal Working Output: 7 -> 0 -> 8 This represents the sum 342 + 465 = 807.
The addTwoNumbers function begins by creating a dummy node as the head of the resulting linked list. The dummy node is initialized with a value of 0, and its next pointer is set to NULL. The function also initializes a pointer called curr to point to the dummy node. The function then proceeds to iterate through both input linked lists, as well as any carryover from the previous addition. At each iteration, it calculates the sum of the current digits from both input linked lists and the carryover, and creates a new node in the resulting linked list with the least significant digit of the sum. The carryover for the next iteration is set to the most significant digit of the sum.
The function continues to iterate until both input linked lists and any carryover are exhausted. At this point, the resulting linked list is complete, and the function returns a pointer to its head. In the main function, the implementation creates two input linked lists and calls the addTwoNumbers function to compute their sum. It then iterates through the resulting linked list and prints out the values of its nodes. Overall, the implementation is a straightforward and efficient solution to the problem of adding two numbers represented as linked lists in C++. It takes advantage of the fact that the input linked lists are already in reverse order to simplify the logic of the addition operation. It also uses a dummy node as the head of the resulting linked list to simplify the handling of edge cases.
We start by creating a dummy node as the head of the new linked list. We also create a pointer curr that points to the current node that we are adding to the new linked list. We initialize carry to 0, which will hold any carryover from the previous addition.We loop through both linked lists and the carryover, adding the corresponding values and carryover from the previous addition.
If either linked list is shorter than the other, we treat the missing nodes as having a value of 0. We create a new node with the sum modulo 10 (since the maximum sum of two digits is 9 + 9 = 18), and set the carryover to the sum divided by 10. We move curr to the newly created node. Finally, we return the next node after the dummy node.