1. 程式人生 > 其它 >Java HelloWorld實現及Java執行原理介紹

Java HelloWorld實現及Java執行原理介紹

題目來源

445. 兩數相加 II

題目詳情

給你兩個 非空 連結串列來代表兩個非負整數。數字最高位位於連結串列開始位置。它們的每個節點只儲存一位數字。將這兩數相加會返回一個新的連結串列。

你可以假設除了數字 0 之外,這兩個數字都不會以零開頭。

示例1:

輸入: l1 = [7,2,4,3], l2 = [5,6,4]
輸出: [7,8,0,7]

示例2:

輸入: l1 = [2,4,3], l2 = [5,6,4]
輸出: [8,0,7]

示例3:

輸入: l1 = [0], l2 = [0]
輸出: [0]

提示:

  • 連結串列的長度範圍為 [1, 100]
  • 0 <= node.val <= 9
  • 輸入資料保證連結串列代表的數字無前導 0

進階: 如果輸入連結串列不能翻轉該如何解決?

題解分析

解法一:連結串列翻轉

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        l1 = reverseList(l1);
        l2 = reverseList(l2);
        ListNode newHead = addTwoList(l1, l2);
        return reverseList(newHead);
    }
    

    private ListNode reverseList(ListNode head){
        ListNode now = head, pre = null;
        while(now != null){
            ListNode next = now.next;
            now.next = pre;
            pre = now;
            now = next;
        }
        return pre;
    }

    private ListNode addTwoList(ListNode list1, ListNode list2){
        ListNode dumyNode = new ListNode(-1);
        ListNode now = dumyNode;
        int carry = 0;
        while(carry != 0 || list1 != null || list2 != null){
            int a = 0, b = 0;
            if(list1 != null){
                a = list1.val;
                list1 = list1.next;
            }
            if(list2 != null){
                b = list2.val;
                list2 = list2.next;
            }
            int sum = carry + a + b;
            carry = sum / 10;
            now.next = new ListNode(sum % 10);
            now = now.next;
        }
        now.next = null;
        return dumyNode.next;
    }
}

解法二:棧

  1. 本題的要求裡需要不能翻轉原先的兩個連結串列來實現連結串列的加法,考慮到連結串列的特性,這裡考慮使用棧來求解。
  2. 棧的特點就是先進後出,所以我們可以先把所有元素分別進棧,然後依次出棧相加,與此同時構造出新的ListNode,將其接到head節點的後面。
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        LinkedList<Integer> sta1 = new LinkedList<>();
        LinkedList<Integer> sta2 = new LinkedList<>();
        while(l1 != null){
            sta1.push(l1.val);
            l1 = l1.next;
        }
        while(l2 != null){
            sta2.push(l2.val);
            l2 = l2.next;
        }
        ListNode dumyNode = new ListNode(-1);
        ListNode now = dumyNode;
        int carry = 0;
        while(carry != 0 || !sta1.isEmpty() || !sta2.isEmpty()){
            int a = 0, b = 0;
            if(!sta1.isEmpty()){
                a = sta1.pop();
            }
            if(!sta2.isEmpty()){
                b = sta2.pop();
            }
            int sum = carry + a + b;
            carry = sum / 10;
            ListNode temp = new ListNode(sum % 10);
            ListNode next = now.next;
            temp.next = next;
            now.next = temp;
        }
        return dumyNode.next;
    }
}