亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

關于從Leetcode算法部分添加兩個數字(要求幾行解釋)

關于從Leetcode算法部分添加兩個數字(要求幾行解釋)

富國滬深 2022-09-22 16:13:31
您將獲得兩個非空鏈接列表,表示兩個非負整數。數字以相反的順序存儲,其每個節點都包含一個數字。將這兩個數字相加,并將其作為鏈接列表返回。您可以假定這兩個數字不包含任何前導零,但數字 0 本身除外。我不明白(公共列表節點添加兩個數字(列表節點l1,列表節點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 -> 8Explanation: 342 + 465 = 807.
查看完整描述

1 回答

?
當年話下

TA貢獻1890條經驗 獲得超9個贊

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 + " ";


          }


}


查看完整回答
反對 回復 2022-09-22
  • 1 回答
  • 0 關注
  • 95 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號