Adding T2 Two Numbers (Medium)

The topic of this lecture is a middle-level question, which is related to the data structure of the linked list. Next topic:
Insert picture description here
The structure of the linked list:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */

The idea is as follows: do
n't say much, look at the picture first!
Insert picture description here
It can be seen from the figure that when we add 342+456, we add from the beginning of the head and continue to the end. Because the val value represented by the head of the head is exactly our lowest bit, so our idea of solving the problem is : The two linked lists are traversed backward from the head node. A new linked list is defined to save the sum of the numbers at the current position, and save the carry value, and add the carry value in the next loop, loop! The end condition of the loop is that the next fields of the l1 linked list and the l2 linked list are both null!
The idea map is as follows!
Insert picture description hereThe above is the idea map, pay attention to a few points!
1. The newly defined linked list must have a reference to it (can be understood as a pointer). Why? Because when adding a value to the linked list, we will re-copy it as the next operation, then the reference to the head node is gone.
2. When do you want to add the next node to the list? That is when the current corresponding l1 and l2 are worthwhile.
3. What happens if it is 1234+234? Assuming that l1 is 1234 and l2 is 234, then 1+null will appear when it reaches thousands, and the program will inevitably report an error, so the operation we perform is if the bit If the number is different, the value of the current position is 0.

Insert picture description hereNext 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) {
        ListNode res=new ListNode(0); //用来表示结果的链表
        ListNode point=res;//表示对res链表的引用,不然再遍历链表的时候会丢失引用
        int up=0; //表示进位的值 不满10则为0,满10+则为1
        while(l1!=null||l2!=null){ //循环开始 终止条件是next域均为null的情况
        //先判断如果当前位无数值  先将当前位补0  
        int x=l1==null?0:l1.val;
        int y=l2==null?0:l2.val;
        int sum=up+x+y;
        up=sum/10;  //如果当前大于10,那么up的值保存到下次相加的时候
        point.next=new ListNode(sum%10); //创建一个新的节点
        point=point.next;//移动到写一个节点
        //看l1和l2的下一个节点是否为空
        if(l1!=null){//只要当前的next不为null,就赋值到下一个
            l1=l1.next;
        }
        l2=(l2==null?l2:l2.next);
        }  
        //如果最高位相加造成了进位,那么up一定为1,因为两个是的next都为null,就跳出了!
        if(up==1){
            point.next=new ListNode(up);
        }
        //因为通过point指针来操纵的链表赋值,所以res的next就是需要返回的值!
        return res.next;
    }
}

The above is the explanation of this topic. If there is something wrong, you are welcome to point it out, thank you! (The pictures are not good, forgive me, after all, programmers who can't draw pictures are not good programmers~)

Guess you like

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