Leetcode-Java #2两数相加

38 阅读1分钟

LeetCodeLogo.png

题目

给你两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。 请你将两个数相加,并以相同形式返回一个表示和的链表。

你可以假设除了数字 0 之外,这两个数都不会以 0 开头。

题解

同时遍历两个链表,逐位计算它们的和,并与当前位置的进位值相加。

官方题解

官方只考虑两个等长的场景了,没考虑有一长一短的两个链表

class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode head = null, tail = null;
        int carry = 0;
        while (l1 != null || l2 != null) {
            int n1 = l1 != null ? l1.val : 0;
            int n2 = l2 != null ? l2.val : 0;
            int sum = n1 + n2 + carry;
            if (head == null) {
                head = tail = new ListNode(sum % 10);
            } else {
                tail.next = new ListNode(sum % 10);
                tail = tail.next;
            }
            carry = sum / 10;
            if (l1 != null) {
                l1 = l1.next;
            }
            if (l2 != null) {
                l2 = l2.next;
            }
        }
        if (carry > 0) {
            tail.next = new ListNode(carry);
        }
        return head;
    }
}

self

加上考虑长短链表


package leetcode.editor.cn;

class 两数相加 {
    public static void main(String[] args) {
        Solution solution = new 两数相加().new Solution();
        ListNode l1 = new ListNode(2, new ListNode(4, new ListNode(3)));
        ListNode l2 = new ListNode(5, new ListNode(6, new ListNode(4)));
        ListNode listNode = solution.addTwoNumbers(l1, l2);
        while (listNode != null) {
            System.out.println(listNode.val);
            listNode = listNode.next;
        }
    }

    public static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
    //leetcode submit region begin(Prohibit modification and deletion)


    class Solution {
        public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            ListNode longHead = getLength(l1) > getLength(l2) ? l1 : l2;
            ListNode shortHead = longHead == l1 ? l2 : l1;

            ListNode curLong = longHead;
            ListNode curShort = shortHead;
            ListNode lastNode = null;
            int curNum = 0;
            int carry = 0;

            while (curShort != null) {
                curNum = carry + curLong.val + curShort.val;
                curLong.val = curNum % 10;
                carry = curNum / 10;

                lastNode = curLong;
                curLong = curLong.next;
                curShort = curShort.next;
            }

            while (curLong != null) {
                curNum = curLong.val + carry;
                curLong.val = curNum % 10;
                carry = curNum / 10;

                lastNode = curLong;
                curLong = curLong.next;
            }

            if (carry != 0) {
                lastNode.next = new ListNode(1);
            }
            return longHead;
        }

        public int getLength(ListNode listNode) {
            int lenght = 0;
            while (listNode != null) {
                lenght++;
                listNode = listNode.next;
            }
            return lenght;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}