(java)leetcode-445 Add Two Numbers II(两数相加 II)

题目描述

给你两个 非空 链表来代表两个非负整数。数字最高位位于链表开始位置。它们的每个节点只存储一位数字。将这两数相加会返回一个新的链表。

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

进阶:

如果输入链表不能修改该如何处理?换句话说,你不能对列表中的节点进行翻转。

示例:

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

思路1:翻转链表

这题是leetcode-2 Add Two Numbers(两数相加)的延伸,该题的处理顺序是从低位到高位进行相加操作,这样的好处是,对于进位的处理比较方便,因为进位本来就是从低位向高位的。这题的难点也就在于从高位向低位如何处理。

其实题目也进行了提示,我们可以先对输入链表进行翻转,那么就能转化为leetcode-2 Add Two Numbers(两数相加),又变成从低位到高位处理了。最后只需要把结果链表再进行一次翻转就是正确答案了。

代码

class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
    	if (l1 == null && l2 == null) return null;
    	l1 = reverseList(l1);
    	l2 = reverseList(l2);

    	return reverseList(addTwoList(l1,l2));
    }

    private ListNode addTwoList(ListNode l1, ListNode l2) {
    	ListNode newHead = new ListNode(0);
    	ListNode cur = newHead;
    	// 进位
    	int carry = 0;
 		while(l1 != null || l2 != null || carry > 0) {
        	int sum = carry;
        	// 短的链表补0
            sum += l1 == null ? 0 : l1.val;
            sum += l2 == null ? 0 : l2.val;
            
            cur.next = new ListNode(sum % 10);

            cur = cur.next;
            if(l1 != null)
                l1 = l1.next;
            if(l2 != null)
                l2 = l2.next;

            // 进位
            carry = sum / 10;
        }
    	return newHead.next;

    }

    // 翻转链表并返回翻转后的头节点
    private ListNode reverseList(ListNode head) {
    	if (head == null || head.next == null) return head;
    	ListNode tail = null;
    	while(head != null) {
    		ListNode temp = head.next;
    		head.next = tail;
    		tail = head;
    		head = temp;
    	}
    	return tail;
    }
}

执行用时:3 ms, 在所有 Java 提交中击败了89.75%的用户
内存消耗:39.7 MB, 在所有 Java 提交中击败了95.83%的用户

思路2:栈

题目给了提示,也给了挑战——这题可以不需要翻转就能处理。
不翻转怎么进行逆序处理呢?自然就能想到使用了——栈总是与逆序息息相关。
我们先把输入链表存入两个栈中,那么出栈顺序就是原来的倒序了,如此一来,又实现了从低位往高位处理。

代码

class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) { 
        Stack<Integer> stack1 = new Stack<>();
        Stack<Integer> stack2 = new Stack<>();
        // 将L1入栈
        while (l1 != null) {
            stack1.push(l1.val);
            l1 = l1.next;
        }
        // 将L2入栈
        while (l2 != null) {
            stack2.push(l2.val);
            l2 = l2.next;
        }
        
        int carry = 0;
        ListNode head = null;

        while (!stack1.isEmpty() || !stack2.isEmpty() || carry > 0) {
            int sum = carry;
            sum += stack1.isEmpty()? 0: stack1.pop();
            sum += stack2.isEmpty()? 0: stack2.pop();
            ListNode node = new ListNode(sum % 10);
            node.next = head;
            head = node;
            carry = sum / 10;
        }
        return head;
    }
}

执行用时:
5 ms, 在所有 Java 提交中击败了65.19%的用户
内存消耗:40 MB, 在所有 Java 提交中击败了95.83%的用户

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值