[LeetCode] 445. 两数相加 II

Posted 怕什么

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[LeetCode] 445. 两数相加 II相关的知识,希望对你有一定的参考价值。

自己想的太复杂了。。。。。

/**
 * 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 pre=null;
        ListNode head=null;
        ListNode l1node=l1,l2node=l2;
        pre.next=head;
        int l1len=0;
        int l2len=0;
        whille(l1node!=null){
            l1len++;
            l1node=l1node.next;
        }
        whille(l2node!=null){
            l2len++;
            l2node=l2node.next;
        }
        if(l1len>l2len){
            int m=l1len-l2len;
            while(m!=0){
                head=l1;
                pre=head;
                head=head.next;
                l1=l1.next; 
                m--; 
            }
        }else{
            int m=l2len-l1len;
            while(m!=0){
                head=l1;
                pre=head;
                head=head.next;
                l2=l2.next; 
                m--; 
            }
        }
        while(l1!=null){
            int c1=l1==null?0:c1.val;
            int c2=l2==null?0:c2.val;
            int sum=(c1+c2)%10;
            int carry=(c1+c2)/10;
            if(carry==1) pre+=1;
            head=sum;
            pre=sum;
            head=head.next;
        }
    }
}
View Code

然而可以用栈放进去,出来就是数字从低位到高位了。

class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) { 
        Stack<Integer> stack1 = new Stack<>();
        Stack<Integer> stack2 = new Stack<>();
        while (l1 != null) {
            stack1.push(l1.val);
            l1 = l1.next;
        }
        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;
    }
}

或者是直接翻转链表,然后按照第2题那样的方法去做更快

/**
 * 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 = reverseList(l1);
        ListNode q = reverseList(l2);
        ListNode cur = 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 = x + y + carry;
            carry = sum/10;
            cur.next = new ListNode(sum%10);
            cur = cur.next;
            if (p != null){
                p = p.next;
            }
            if (q != null){
                q = q.next;
            }
        }
        if (carry == 1){
            cur.next = new ListNode(carry);
        }
        return reverseList(dummyHead.next);
    }
    public static ListNode reverseList(ListNode cur) {
        ListNode pre = null;
        while (cur != null) {
            ListNode temp = cur.next;
            cur.next = pre;
            pre = cur;
            cur = temp;
        }
        return pre;
    }
}

 

以上是关于[LeetCode] 445. 两数相加 II的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode Java刷题笔记—445. 两数相加 II

LeetCode 445 两数相加 II

(链表专题) 445. 两数相加 II ——Leetcode每日一题

LeetCode 445. 两数相加 II Add Two Numbers II (Medium)

LeetCode 445. 两数相加 II Add Two Numbers II (Medium)

LeetCode 0445. 两数相加 II