LeetCode 445. 兩數相加 II (鏈表) 我的解題記錄

題目

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

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

 

進階:

如果輸入鏈表不能修改該如何處理?換句話說,你不能對列表中的節點進行翻轉。

 

示例:

輸入:(7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
輸出:7 -> 8 -> 0 -> 7

遞歸解法 先給長度短的補上0

	int carry = 0;
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {   //遞歸做法 給長度少的補0
        if (l1==null)
            return l2;
        if (l2==null)
            return l1;
        int length1 = 0;
        int length2 = 0;
        ListNode node = l1;
        while (node!=null){
            length1++;
            node = node.next;
        }
        node = l2;
        while (node!=null){
            length2++;
            node = node.next;
        }
        ListNode out ;
        if (length1!=length2){
            ListNode listNode = new ListNode(0);
            ListNode go =listNode;
            int count = Math.abs(length1-length2)-1;
            while (count>0){
                listNode.next = new ListNode(0);
                listNode = listNode.next;
                count--;
            }
            listNode.next = length1>length2?l2:l1;
            if (length1>length2){
                out  = add(l1,go);
            }else {
                out = add(go,l2);
            }
        }else
            out =  add(l1,l2);
        if (carry!=0){
            ListNode carryNode = new ListNode(carry);
            carryNode.next = out;
            out = carryNode;
        }
        return out;
    }
    public ListNode add(ListNode l1, ListNode l2){
        int val;
        if (l1.next==null&&l2.next==null){
             val = l1.val + l2.val;
        }else {
            addTwoNumbers(l1.next,l2.next);
            val = l1.val + l2.val + carry;
        }
        carry = val / 10; //計算進位
        val = carry==0?val:val-10; //該位的最終表示
        l1.val = val;
        return l1;
    }

迭代做法 利用棧迭代 當棧無數據時用0代替

 	public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        if (l1==null)
            return l2;
        if (l2==null)
            return l1;
        Stack<Integer> stack1 = new Stack<>();
        Stack<Integer> stack2 = new Stack<>();
        int carry = 0;
        while (l1!=null){
            stack1.add(l1.val);
            l1 = l1.next;
        }
        while (l2!=null){
            stack2.add(l2.val);
            l2 = l2.next;
        }
        ListNode  out = null;
        while (!(stack1.isEmpty()&&stack2.isEmpty())){
            int n1 = stack1.isEmpty()?0:stack1.pop();
            int n2 = stack2.isEmpty()?0:stack2.pop();
            int sum = n1 + n2 + carry;
            carry = sum / 10;
            if (sum>9){
                sum = sum - 10;
            }
            ListNode node = new ListNode(sum);
            node.next = out;
            out = node;
        }
        if (carry!=0){
            ListNode carryNode = new ListNode(carry);
            carryNode.next = out;
            out = carryNode;
        }
        return out;
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章