Leetcode(二):Add Two Numbers
博客专区 > Ambitor 的博客 > 博客详情
Leetcode(二):Add Two Numbers
Ambitor 发表于2年前
Leetcode(二):Add Two Numbers
  • 发表于 2年前
  • 阅读 49
  • 收藏 0
  • 点赞 1
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

摘要: You are given two linked lists representing two non-negative numbers. 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.

Add Two Numbers

题目

Total Accepted: 137297  Total Submissions: 599150  Difficulty: Medium

You are given two linked lists representing two non-negative numbers. 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.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)

Output: 7 -> 0 -> 8

Subscribe to see which companies asked this question

解题思路

任意选择一个Node进行迭代,迭代中有几种可能 1、Node1个数少于Node2 2、Node1个数和Node2相等 3、Node1个数大于Node2 分别进行考虑,计算进位和取模,迭代过程中改变Node1及Node2的偏移量,然后从当前偏移量Node2开始Node2的迭代。

代码

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            if(l1==null||l2==null)return null;
            int multiple=0;
            ListNode result=null,nextNode=null;//用来表示结果与下一个Node
            while(l1!=null){
                int n1=0,n2=0,sum=0;
                n1= l1.val;
                if(l2!=null){
                    n2= l2.val;
                    sum=n1+n2;
                    sum+=multiple;
                    multiple=0;
                    if(sum>9){
                       multiple=sum/10;
                       sum=sum%10;
                    }
                    if(result==null){
                        result=new ListNode(sum);
                        nextNode=result;
                    }
                    else{ 
                        nextNode.next=new ListNode(sum);
                        nextNode=nextNode.next;
                    }
                    l2=l2.next;//l2的指针随l1偏移
                    l1=l1.next;
                }else{
                    n1+=multiple;
                    multiple=0;
                    if(n1>9){
                        multiple=n1/10;
                        n1=n1%10;
                    }
                    nextNode.next=new ListNode(n1);
                    nextNode=nextNode.next;
                    l1=l1.next;
                }
            }
            while(l2!=null){//如果l1链表长度少于l2链表长度
                int n2= l2.val;
                n2+=multiple;
                multiple=0;
                if(n2>9){
                    multiple=n2/10;
                    n2=n2%10;
                }
                nextNode.next=new ListNode(n2);
                nextNode=nextNode.next;
                l2=l2.next;
            }
            if(multiple!=0){//迭代完Node1、Node2 最后如果进位不为0 则加一位
                nextNode.next=new ListNode(multiple);
                nextNode=nextNode.next;
            }
            return result;
        }
    }

复杂度

O(n)的时间 O(n)的内存

测试结果

其他思路

后面看到有用递归做这个题目的思路,代码会精简很多,大家可以尝试下

注:版权所有转载请注明出处http://my.oschina.net/ambitor/blog/662824,作者:Ambitor


标签: leetcode 算法 java
共有 人打赏支持
粉丝 70
博文 28
码字总数 28201
×
Ambitor
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: