Give you two non empty linked lists to represent two non negative integers. They store each number in reverse order, and each node can store only one number.

Please add the two numbers and return a linked list representing sum in the same form.

You can assume that neither number starts with 0 except the number 0.

Input: l1 = [2,4,3], l2 = [5,6,4]

Output: [7,0,8]

Explanation: 342 + 465 = 807

Link: https://leetcode-cn.com/problems/add-two-numbers

Problem solving ideas

The problem of adding linked lists is very simple. You only need to write the expression to solve the problem at a glance. Take the example of this problem as an example: (2 - > 4 - > 3) + (5 - > 6 - > 4) = (7 - > 0 - > 8). According to the calculation process of addition, we know that we should start from the low order, that is, we should calculate 2 + 5 = 7 first, so the idea comes:

First, take out the lowest order of the two numbers on the left and right sides of "+", that is

let val1 = l1.val let val2 = l2.val

Secondly, find their sum and take it as the lowest bit of the output result (since the output result is saved in the form of linked list, we should write it in this form)

let sum = new ListNode('0') sum.next = new ListNode(""Results" % 10)//The reason why we want "result"% 10 is that our calculation result may be greater than 10, so we need to take the remainder

Therefore, the preliminary code is as follows:

var addTwoNumbers = function(l1, l2) { let sum = new ListNode('0') // Create a header linked list to save the results let head = sum // Save the location of the head linked list for the last linked list return while (l1 || l2) {//Execute the following logic on the premise that one of the two linked lists exists let val1 = l1.val let val2 = l2.val let r1 = val1 + val2//Sum sum.next = new ListNode(r1 % 10)//Next node of sum sum = sum.next //sum points to the next node if (l1) l1 = l1.next //l1 points to the next node to calculate the value of the second node if(l2) l2 = l2.next //l2 points to the next node to calculate the value of the second node } return head.next //Returns the calculation result. The reason for using head Next is because the first node saved in the head is "0" defined at the beginning };

The following is the start of optimization. As a college student, the first thing we should know is that the addition operation has carry, so consider adding carry to the code

let addOne = 0 //carry let sum = new ListNode('0') let head = sum while (addOne || l1 || l2) {//Execute the following logic on the premise that one of the carry or two linked lists exists let val1 = l1.val let val2 = l2.val let r1 = val1 + val2 + addOne//Sum addOne = r1 >= 10 ? 1 : 0 // If the sum result > = 10, the carry is 1, otherwise it is 0 sum.next = new ListNode(r1 % 10) sum = sum.next if (l1) l1 = l1.next if(l2) l2 = l2.next } return head.next };

Writing the above code is close to success, but it is still a step away from success. When we submit, we can't pass all cases. The case that can't pass is the addition of: [9999] and [99], because in the process of adding l1 and l2, when the pointer points to the third 9, the 9 of l1 exists, but the 9 of l2 does not exist, that is, it is null, Therefore, an error will be reported if the value cannot be added, so we need to carry out one-step optimization. If the value does not exist, set it to 0, and then add it. The completion code is as follows:

var addTwoNumbers = function(l1, l2) { let addOne = 0 let sum = new ListNode('0') let head = sum while (addOne || l1 || l2) { let val1 = l1 !== null ? l1.val : 0 // Optimization point let val2 = l2 !== null ? l2.val : 0 //Optimization point let r1 = val1 + val2 + addOne addOne = r1 >= 10 ? 1 : 0 sum.next = new ListNode(r1 % 10) sum = sum.next if (l1) l1 = l1.next if (l2) l2 = l2.next } return head.next };

This solution is the most direct and universal. In the future, this idea can be adopted no matter how many numbers are added. I think if you learn more about the algorithm, you will draw inferences from one example slowly

code

/** * Definition for singly-linked list. * function ListNode(val, next) { * this.val = (val===undefined ? 0 : val) * this.next = (next===undefined ? null : next) * } */ /** * @param {ListNode} l1 * @param {ListNode} l2 * @return {ListNode} */ var addTwoNumbers = function(l1, l2) { let addOne = 0 let sum = new ListNode('0') let head = sum while (addOne || l1 || l2) { let val1 = l1 !== null ? l1.val : 0 let val2 = l2 !== null ? l2.val : 0 let r1 = val1 + val2 + addOne addOne = r1 >= 10 ? 1 : 0 sum.next = new ListNode(r1 % 10) sum = sum.next if (l1) l1 = l1.next if (l2) l2 = l2.next } return head.next };

Author: Afeng Xiu

Link: https://leetcode-cn.com/problems/add-two-numbers/solution/liang-ge-shu-xiang-jia-zui-rong-yi-li-jie-de-jie-f/

The linked list needs to specify the position of the head change and set another pointer. Pay attention to the last carry and output 0 to remove the head next

Write your own answer:

package com.kuang.LeetcodeStuddy; public class leetcode2_1 { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode prev = new ListNode(0); int carry = 0; ListNode cur = prev; while(l1!=null || l2!=null){ int x= l1 !=null ? l1.val : 0; int y = l2 !=null ? l2.val : 0; int sum = x + y + carry; carry = sum / 10; sum = sum % 10; cur.next = new ListNode(sum); cur = cur.next; if(l1 !=null){ l1 = l1.next; } if(l2 !=null){ l2 = l2.next; } } if(carry == 1){ cur.next = new ListNode(carry); } return prev.next; } public static void main(String[] args) { //: l1 = [2,4,3], l2 = [5,6,4] ListNode i1 = new ListNode(0); ListNode l1 = i1; l1.next = new ListNode(2); l1 = l1.next; l1.next = new ListNode(4); l1 = l1.next; l1.next = new ListNode(3); l1 = l1.next; System.out.println(i1.next); ListNode i2 = new ListNode(0); ListNode l2 = i2; l2.next = new ListNode(5); l2 = l2.next; l2.next = new ListNode(6); l2 = l2.next; l2.next = new ListNode(4); l2 = l2.next; System.out.println(i2.next); leetcode2 leetcode2 = new leetcode2(); ListNode listNode = leetcode2.addTwoNumbers(i1.next, i2.next); System.out.println(listNode); } }

ListNode class

package com.kuang.LeetcodeStuddy; public class ListNode { int val; ListNode next; ListNode() {} ListNode(int val) { this.val = val; } ListNode(int val, ListNode next) { this.val = val; this.next = next; } @Override public String toString() { return "ListNode{" + "val=" + val + ", next=" + next + '}'; } }