One for Xiaobai.
Blasting, there's nothing clever.
Refer to the official explanation and other great gods. I tried many times to get it out.
Title:
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. Example 2: Input: l1 = [0], l2 = [0] Output:[0] Example 3: Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9] Output:[8,9,9,9,0,0,0,1] Tips: The number of nodes in each linked list is in the range [1, 100] within 0 <= Node.val <= 9 The number represented in the title data guarantee list does not contain leading zeros
/** * Definition for singly-linked list. * 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; } * } */ class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode lh = new ListNode(); ListNode lr = lh; int c = 0; while (l1 != null || l2 != null) { if (l1 == null) { if (l2.val + c < 10) { lr.val = l2.val + c; c = 0; l2 = l2.next; } else { lr.val = l2.val + c - 10; c = 1; l2 = l2.next; } if(l2 != null || c > 0){ lr.next = new ListNode(); lr = lr.next; } continue; } if (l2 == null) { if (l1.val + c < 10) { lr.val = l1.val + c; c = 0; l1 = l1.next; } else { lr.val = l1.val + c - 10; c = 1; l1 = l1.next; } if(l1 != null || c > 0){ lr.next = new ListNode(); lr = lr.next; } continue; } if (l1 != null && l2 != null) { if (l1.val + l2.val + c < 10) { lr.val = l1.val + l2.val+c; l1 = l1.next; l2 = l2.next; c = 0; } else { lr.val = l1.val + l2.val + c - 10; l1 = l1.next; l2 = l2.next; c = 1; } if(l1 != null || l2 != null || c > 0){ lr.next = new ListNode(); lr = lr.next; } continue; } } if(c>0){ lr.val = 1; } return lh; } }
Explosive cracking.
Problem solving ideas:
At the beginning, when I saw the title, my first idea was to turn it into a number, and then I looked at the requirements. The length would certainly exceed, which is not feasible.
Then I find the reverse order. I'll add it directly.
However, there are some cases: if the sum of the same digits of the two linked lists is greater than 10, it is necessary to carry, and the carry may encounter continuous carry, and then exceed the longest length of the two linked lists.
Finally, I changed it many times and wrote this solution.
First, two linked lists are defined, one as the total linked list, and the other carries with l1 and l2. Finally, the results are returned through the total.
Loop, provided that one of the two linked lists is not empty.
In the cycle, there are three cases: l1 is empty, l2 is empty, and neither is empty.
In the loop, the carry linked list is carried only when the carry ID (c) is not 0 or l1 and l2 are not empty, otherwise there will be one more 0 at the end.
In three cases, L1 and L2 are carried separately.
That's about it.
Situations encountered:
1. At the beginning, only one linked list was defined. In the result, only the last one was returned. After thinking for a long time, there was no way. Finally, after referring to the writing of the boss, it was found that the linked list must return to the head node, and all the subsequent nodes can be obtained.
2. In case of continuous carry, either one more bit or one less bit is entered.
3. When L1 and L2 are null, a null pointer will be reported when using next.
4. Using next directly will also report null pointers. You need to instantiate a node before copying.
The first time I wrote this record, the layout was a little messy, and the writing was a little messy. I'll try to improve it next time.