2 Add two numbers (linked list)

1. Problem description:

Two non-empty linked lists are given to represent two non-negative integers. Among them, their respective digits are stored in reverse order, and each of their nodes can only store one digit. If we add these two numbers together, a new linked list will be returned to represent their sum. You can assume that except for the number 0, neither of these numbers will start with 0.

Examples:

Input: (2-> 4-> 3) + (5-> 6-> 4)
Output: 7-> 0-> 8
Reason: 342 + 465 = 807

Source:
Link (LeetCode) : https://leetcode-cn.com/problems/add-two-numbers

2. Thinking analysis:

① The first thing is to understand the meaning of the question. You can know that the question mainly solves the result of adding the corresponding positions of the two linked lists from left to right, so it is relatively easy to understand. We traverse the two linked lists in the loop. When one of the two linked lists is not empty, the loop is executed, because such a situation may occur, such as 9-> 9 + 1, so the processing is completed when both linked lists are empty. Use one ListNode type variable to represent the previous node of the node that is about to be generated, using this variable to connect elements will be more convenient

② You can use a variable to record the last carry, and then add the elements at the corresponding positions of the two linked lists while traversing the linked list, and update the value of the carry variable at the same time. If one of the linked lists is empty, only the other linked list is updated. It can be compared with the carry situation. Finally, determine whether the carry is 1. If it is 1, then you need to create a new node because it is a carry before, and you need to connect it.

③ In addition to the new node above, you can also optimize it. You only need to modify the value of the linked list on the original basis so that you can not create a new node of the linked list. Use a ListNode to record the predecessor node. This is good when a linked list is empty Connect the next pointer field of pre to another linked list (the two linked lists have length), then update the value and carry of the other linked list in the loop and finally process the last carry.

3. The code is as follows:

class Solution{
   public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int carry = 0;
        if (l1 == null && l2 == null) return null;
        if (l1 == null && l2 != null) return l2;
        if (l1 != null && l2 == null) return l1;
        ListNode pre = null;
        ListNode head = null;
        while (l1 != null || l2 != null){
            /*即将要插入的节点*/
            ListNode newNode;
            if(l1 != null && l2 != null){
                int curVal = l1.val + l2.val + carry;
                carry = curVal / 10;
                newNode = new ListNode(curVal % 10);
                if (pre == null){
                    pre = newNode;
                    head = newNode;
                }
                else {
                    pre.next = newNode;
                    pre = newNode;
                }
                l1 = l1.next;
                l2 = l2.next;
            }
            else if (l1 != null){
                int curVal = l1.val + carry;
                carry = curVal / 10;
                newNode = new ListNode(curVal % 10);
                if (pre == null) {
                    pre = newNode;
                    head = newNode;
                }
                else {
                    pre.next = newNode;
                    pre = newNode;
                    l1 = l1.next;
                }
            }else if (l2 != null){
                int curVal = l2.val + carry;
                carry = curVal / 10;
                newNode = new ListNode(curVal % 10);
                if (pre == null) {
                    pre = newNode;
                    head = newNode;
                }
                else {
                    pre.next = newNode;
                    pre = newNode;
                    l2 = l2.next;
                }
            }
        }
        if (carry == 1){
            ListNode last = new ListNode(1);
            pre.next = last;
        }
        return head;
    }
}

Optimized code:

class Solution{
 public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int carry = 0;
        if (l1 == null && l2 == null) return null;
        if (l1 == null && l2 != null) return l2;
        if (l1 != null && l2 == null) return l1;
        ListNode pre = null, head = l1;
        int curVal = 0;
        while (l1 != null && l2 != null){
            curVal = l1.val + l2.val + carry;
            l1.val = curVal % 10;
            carry = curVal / 10;
            /*pre指针是为了避免l1比较短的情况*/
            pre = l1;
            l1 = l1.next;
            l2 = l2.next;
        }
        while (l1 != null){
            curVal = l1.val + carry;
            l1.val = curVal % 10;;
            carry = curVal / 10;
            pre = l1;
            l1 = l1.next;
        }
        if (l1 == null && l2 != null) pre.next = l2;
        while (l2 != null){
            curVal = l2.val + carry;
            l2.val = curVal % 10;
            carry = curVal / 10;
            pre = l2;
            l2 = l2.next;
        }
        if (carry == 1) {
            ListNode last = new ListNode(1);
            pre.next = last;
        }
        return head;
    }
}

 

Published 569 original articles · Like 153 · Visits 590,000+

Guess you like

Origin blog.csdn.net/qq_39445165/article/details/105352106