LeetCode算法题:两数相加

需求

两数相加:


LeetCode算法题:两数相加_第1张图片
需求

思路

链表是倒序存储数值,432 在链表存储是2-3-4,当求和之后,还需将值转成链表,
1.根据链表,得到链表存储的值,用集合存储
2.将集合中存储的值,转换成真正的数值
3.求和
4.将值转成集合
5.将集合转成链表

实现

package jk.zmn.suanfa; /**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
   2  4 5
    542

  思路:
   将链表转成数组,
   根据数组生成值
   计算总和
   生成链表

 */
import java.util.*;

public class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {

        ArrayList integers = listnodetoList(l1);
        Collections.reverse(integers);
        ArrayList integers1 = listnodetoList(l2);
        Collections.reverse(integers1);
        int i = listtoNumber(integers);
        int i1 = listtoNumber(integers1);
        int sum = i +i1;
        ArrayList integers2 = numbertoList(sum);
        ListNode head = listtolistnode(integers2);

        return head;
    }


    //链表转成数组
    public ArrayList listnodetoList(ListNode l1){
        ArrayList list1 = new ArrayList();
        while(l1 != null){
            list1.add(l1.val);
            l1 = l1.next;
        }
        return list1;
    }

    //根据集合生成值
    public int listtoNumber(ArrayList list){
        int num=0;
        for(int i=0;i numbertoList(int number){

        ArrayList integers = new ArrayList<>();
        while (number / 10 >= 0 ){
            int gewei = number % 10  ;
            integers.add(gewei);
            if (number /10 == 0){
                break;
            }
            number = number/10;
        }
        return integers;
    }

    //集合转链表
    public ListNode listtolistnode(ArrayList list1){
        ListNode head = null;
        ListNode rear = null;
        for(int i=0;i < list1.size(); i++){

            if ( head == null)
            {
                head = new ListNode(list1.get(i));
                rear = head;
                continue;
            }
            rear.next = new ListNode(list1.get(i));
            rear = rear.next;
        }
        ListNode temp = head;
        while (temp != null){
            System.out.println(temp.val+" ");
            temp = temp.next;
        }
        System.out.println();
        return head;
    }

    public static void main(String[] args){
        ListNode n1 = new ListNode(9);

        ListNode n4 = new ListNode(1);
        ListNode n5 = new ListNode(9);
        ListNode n6 = new ListNode(9);
        n4.next = n5;
        n5.next = n6;

        //new Solution().swap(n1);
        ListNode listNode = new Solution().addTwoNumbers(n1, n4);
        while (listNode != null){
            System.out.println(listNode.val);
            listNode = listNode.next;
        }

    }

    static class ListNode {
        int val;
        ListNode next;
        ListNode(int x) { val = x; }
    }
}

效果如图:

public static void main(String[] args){
        ListNode n1 = new ListNode(9);

        ListNode n4 = new ListNode(1);
        ListNode n5 = new ListNode(9);
        ListNode n6 = new ListNode(9);
        n4.next = n5;
        n5.next = n6;

        //new Solution().swap(n1);
        ListNode listNode = new Solution().addTwoNumbers(n1, n4);
        while (listNode != null){
            System.out.println(listNode.val);
            listNode = listNode.next;
        }

    }
LeetCode算法题:两数相加_第2张图片
image.png

QQ交流群:552113611
更多资源与源码,请加群

你可能感兴趣的:(LeetCode算法题:两数相加)