LeeCode 两数相加(逆序链表)

package Test;


import java.util.ArrayList;
import java.util.List;

public class TEst {
    public static void main(String[] args) {

        ListNode node1 = new ListNode(1);
        ListNode node11 = node1;
        ListNode node2 = new ListNode(1);
        ListNode node22 = node2;
        for (int i = 2; i < 5; i++) {
            node11.next = new ListNode(i);
            node11 = node11.next;
            node22.next = new ListNode(i);
            node22 = node22.next;
        }

        Solution soluver = new Solution();
        ListNode node = soluver.addTwoNumbers(node1 , node2);

        while(node!=null) {
            System.out.println(node.val);
            node = node.next;
        }
    }
}
//Definition for singly-linked list.

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

class Solution {

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ArrayList list1 = new ArrayList<>();
        ArrayList list2 = new ArrayList<>();
        long sum1 = 0;
        long sum2 = 0;

        while (l1 != null) {
            list1.add(l1.val);
            l1 = l1.next;
        }
        while (l2 != null) {
            list2.add(l2.val);
            l2 = l2.next;
        }

        for (int i = 0; i < list1.size(); i++) {
            sum1 += list1.get(i) * Math.pow(10, i);
        }

        for (int i = 0; i < list2.size(); i++) {
            sum2 += list2.get(i) * Math.pow(10, i);
        }

        long sum = sum1 + sum2;
        int length = new Long(sum).toString().length();


        ListNode fNode = new ListNode( (int)(sum % 10) );
        sum = sum / 10;
        ListNode nowNode = fNode;
//           nowNode = nowNode.next;

        for (int i = length; i > 1; i--) {
            nowNode.next = new ListNode( (int)(sum % 10) );
            sum = sum / 10;
            nowNode = nowNode.next;
        }
        return fNode;
    }
}

这是一开始写的,,但是,后来发现提交的时候链表的情况是这样的

没办法,只能重新写了==

 

自己的垃圾代码

/**
 * 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 fNode;
        int symbol = 0;
        fNode = new ListNode( (l1.val + l2.val) % 10);
        ListNode nowNode = fNode;
        int sum = 0;

        //判断是否向高位进位
        if( l1.val + l2.val >= 10) {
            symbol = 1;
        }else {
            symbol = 0;
        }
        l1 = l1.next;
        l2 = l2.next;

        while(l1 != null || l2 != null) {
            if (l1 != null && l2 != null) {
                sum = (l1.val + l2.val);
                l1 = l1.next;
                l2 = l2.next;
            }
            else if (l2 == null) {
                sum = l1.val;
                l1 = l1.next;
            }
            else if (l1 == null) {
                sum = l2.val;
                l2 = l2.next;
            }


            if (symbol ==1){
                sum++;
            }

            nowNode.next = new ListNode( sum % 10);
            nowNode = nowNode.next;


            if( sum >= 10) {
                symbol = 1;
            }else {
                symbol = 0;
            }
        }

        if (symbol == 1) {
            nowNode.next = new ListNode(1);
        }
        return fNode;
    }
}

根据大佬的思路进行了优化

package Test;


import java.util.ArrayList;
import java.util.List;

public class TEst {
    public static void main(String[] args) {

        ListNode node1 = new ListNode(1);
        ListNode node11 = node1;
        ListNode node2 = new ListNode(1);
        ListNode node22 = node2;
        for (int i = 2; i < 5; i++) {
            node11.next = new ListNode(i);
            node11 = node11.next;
        }
        for (int i = 2; i < 4; i++) {
            node22.next = new ListNode(i);
            node22 = node22.next;
        }

        Solution soluver = new Solution();
        ListNode node = soluver.addTwoNumbers(node1 , node2);

        while(node!=null) {
            System.out.println(node.val);
            node = node.next;
        }
    }
}
//Definition for singly-linked list.

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

class Solution {

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int num = (l1.val + l1.val) / 10;
        ListNode fNode = new ListNode((l1.val + l1.val) % 10);
        ListNode nowNode = fNode;

        l1 = l1.next;
        l2 = l2.next;

        while( !(l1 == null && l2 == null && num == 0)) {
            int l1Num = l1 == null ? 0 : l1.val;
            int l2Num = l2 == null ? 0 : l2.val;
            int sum = l1Num + l2Num + num;      //num从低位满10进的一位
            num = sum / 10;
            nowNode.next = new ListNode(sum % 10);
            nowNode = nowNode.next;
            if (l1 != null) {
                l1 = l1.next;
            }
            if (l2 != null) {
                l2 = l2.next;
            }
        }
        return fNode;
    }
}


结果,,事实证明运算时间不止和代码有关系,,,,

LeeCode 两数相加(逆序链表)_第1张图片

你可能感兴趣的:(LeeCode)