Leetcode题目训练日记(Java实现):#2. Add Two Numbers

 

 

一、题目

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Example:

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.

二、本人答案,Runtime:29ms

//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 p=l1;
        ListNode q=l2;
        int jinwei=0;
        while (p!=null&&q!=null){
            int tmp;
            if (jinwei==1){
                tmp=p.val+q.val+1;
            }else {
                tmp = p.val + q.val;
            }
            if(tmp>9){
                jinwei=1;
                tmp%=10;
            }else {
                jinwei=0;
            }
            p.val=tmp;
            q.val=tmp;
            //当p,q等长时,为了避免丢失l1,l2中最后一个非空节点的指针,
            //从而导致因进位而new的新节点,无法正确连接到l1或l2末尾
            if(p.next==null&&q.next==null){
                if (jinwei==1){
                    p.next=new ListNode(1);
                }
                return l1;
            }
            p=p.next;
            q=q.next;
        }
        //下面的代码是在说:p,q谁长处理谁,并最终用其做返回
        if(p!=null){
            while (jinwei==1){
                p.val+=1;
                if(p.val>9){
                    if(p.next==null){
                        p.next=new ListNode(1);
                        jinwei=0;
                    }
                    p.val%=10;
                    p=p.next;
                }else {
                    jinwei=0;
                }
            }
            return l1;
        }
        if (q!=null){
            while (jinwei==1){
                q.val+=1;
                if(q.val>9){
                    if(q.next==null){
                        q.next=new ListNode(1);
                        jinwei=0;
                    }
                    q.val%=10;
                    q=q.next;
                }else {
                    jinwei=0;
                }
            }
            return l2;
        }
        return null;
    }
}

代码看起来有点长,啰里啰嗦的,但实际思路很简单:从个位开始,将两条链表上的数相加结果存放在做相加的两个节点中去,最终返回较长的那个链表(两个链表等长时随意返回一条)。
这里最复杂的问题就是处理进位问题,例如:1+9999,在第一个while结束之后,还要利用循环依次进位,并在最后new一个新节点存放最后的进位;5+5,要在p,q变为null之前,new新节点,并连接到l1末尾。
时间复杂度:O(n),空间复杂度:O(1)

三、优秀答案,Runtime:23ms

class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode head = new ListNode(0);
        int carry = 0;
        while(l1!=null||l2!=null||carry>0)
        {
            ListNode itr = head;
            while(itr.next!=null)
                itr = itr.next;
            int sum = ( (l1==null ? 0 : l1.val) + (l2==null ? 0 : l2.val) + carry);
            carry = sum/10;
            ListNode temp = new ListNode(sum%10);
            itr.next = temp;
            if(l1!=null)
                l1 = l1.next;
            if(l2!=null)
                l2 = l2.next;
        }
        
        return head.next;
    }
}

 

你可能感兴趣的:(Leetcode每日训练)