关于从Leetcode算法部分加两个数字(要求几行说明)

时间:2019-04-05 13:42:33

标签: java algorithm

为您提供了两个非空链接列表,它们表示两个非负整数。这些数字以相反的顺序存储,并且它们的每个节点都包含一个数字。将两个数字相加,然后将其作为链表返回。

您可能会假定两个数字除了数字0本身之外都不包含任何前导零。

我不理解(public ListNode addTwoNumbers(ListNode l1,ListNode l2))为什么为此给出两个名称。谢谢

     /**
     * 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 dummyHead = new ListNode(0);
        ListNode p = l1, q = l2, curr = dummyHead;

        int carry = 0;
        while (p != null || q != null) {
            int x = (p != null) ? p.val : 0;
            int y = (q != null) ? q.val : 0;
            int sum = carry + x + y;
            carry = sum / 10;
            curr.next = new ListNode(sum % 10);
            curr = curr.next;
            if (p != null) p = p.next;
            if (q != null) q = q.next;
        }
        if (carry > 0) {
            curr.next = new ListNode(carry);
        }
        return dummyHead.next;
    }

    }

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.

1 个答案:

答案 0 :(得分:0)

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

        public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            ListNode dummyHead = new ListNode(0);
            ListNode p = l1, q = l2, curr = dummyHead;

            int carry = 0;
            while (p != null || q != null) {
                int x = (p != null) ? p.val : 0;
                int y = (q != null) ? q.val : 0;
                int sum = carry + x + y;
                carry = sum / 10;
                curr.next = new ListNode(sum % 10);
                curr = curr.next;
                if (p != null) p = p.next;
                if (q != null) q = q.next;
            }
            if (carry > 0) {
                curr.next = new ListNode(carry);
            }
            return dummyHead.next;
        }

    public static void main(String[] args) {

            Solution solution = new Solution();

        ListNode l1 = new ListNode(2);
        ListNode nextl11 = new ListNode(4);
        ListNode nextl12 = new ListNode(3);
        l1.next = nextl11;
        nextl11.next = nextl12;



        ListNode l2 = new ListNode(5);
        ListNode nextl21 = new ListNode(6);
        ListNode nextl22 = new ListNode(4);
        l2.next = nextl21;
        nextl21.next = nextl22;
        System.out.println(solution.addTwoNumbers(l1, l2));
    }

    }




public class ListNode {
          int val;
          ListNode next;
          ListNode(int x) { val = x; }
          public String toString() {
              if (next != null)
              return "" + val + " " + next.toString();
              else
                  return "" + val + " ";

          }

}