LeetCode's T445 Two Number Addition II (Medium)

This topic is explained first two numbers together T445 II (moderate) on LeetCode, adding two numbers which T2 (medium) refer to: https: //blog.csdn.net/Pzzzz_wwy/article/details/105519719
contact Next topic:
Insert picture description here
The definition of nodes Insert picture description here
:
Let’s not talk about the idea: First of all, we have to know that if we pass the traversal, then the first value is the higher value, and then the lower value is obtained in turn, and then added by position Then write the value to a new linked list, and then explain the specific steps:
First , we use the stack to store the values ​​obtained from the two linked lists in turn. Why, because we first extract the higher number, and then push the stack. Therefore, the top of the stack is the low-order number, so we pop the stack to be the low-order number, which happens to be added from the low-order, which conforms to the rule of adding two numbers! As shown in the figure:
Insert picture description hereThis way we can get the lowest two digits, and then create a new Node, and then put it into the linked list in turn.
So how to put it into the linked list, we must know that the value we get first is the low-order number, and if we directly insert it into the linked list, the number of the linked list position is the highest number, so we choose the head interpolation method to compare the linked list Insert, as shown in the figure:
Insert picture description here
We use the above method to operate the linked list, so that the obtained value is directly added to the linked list, and at the same time, the specified linked list is directly returned (the head of the linked list is the highest bit).
Next on the code:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        //定义两个栈来进行存储链表
        Stack<Integer> st1=new Stack();
        Stack<Integer> st2=new Stack();
        //将两个链表的值压栈
        while(l1!=null){
            st1.push(l1.val);
            l1=l1.next;
        }
        while(l2!=null){
            st2.push(l2.val);
            l2=l2.next;
        }
        //定义链表
        ListNode head=null;
        //存储每次相同位相加时候需要的进位
        int cur=0;
        while(!st1.empty()||!st2.empty()||cur>0){ //只要任意一个栈中还有数,或者加完后cur为1(此时为最高位相加后进位但是栈空的情况!)
            int s1=st1.empty()?0:st1.pop();//如果其中一个栈空了但是取值则会是null,报异常,所以置为0
            int s2=st2.empty()?0:st2.pop();
            int sum=(s1+s2+cur)%10;
            cur=(s1+s2+cur)/10;
            //利用头插法,新的节点的next指向链表,再将head指向对node的引用
            ListNode node=new ListNode(sum);
            node.next=head;
            head=node;
        }
        return head;
    }
}

There are a few things to note:
1. If the elements of the two stacks are not the same, when one is still there, and the other is empty, the sum will inevitably take the value of null, an error is reported, so we need to add judgment, If the stack is empty, then the current value cannot be null, but 0.
2. If the addition to the last highest bit produces a carry situation, then we only need to add the case of cur>0 to the loop condition, because this is because the last loop cur=(s1+s2+cur)/10 is If it is greater than 0, it is actually 1, so a carry occurs.
Insert picture description here
The above is the solution to this problem. Use the stack to save the value in the linked list, and then use the header interpolation to insert the linked list when adding in the same position! If you have any problems or doubts, please leave a message below! Thank you!

Guess you like

Origin blog.csdn.net/Pzzzz_wwy/article/details/105623573