zoukankan      html  css  js  c++  java
  • 【LeetCode】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.

    Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
    Output: 7 -> 0 -> 8

    分析:

    首先便是遍历链表的每个节点。为了减少存储空间,可以直接把结果存在l1链表中。

    遍历和相加都很简单,主要就是要注意进位的问题。第一个节点的进位设为0,及carry初始化为0。后面在相加过程中遇到进位,便改变carry的值。必须要注意的是,最后一个进位的处理。如果遍历到最后一个节点,相加后还是有进位,那要重新分配一个节点,用于存储产生的进位值。

    以下是用java实现的主要函数,测试类没给出。

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            if(l1 == null){
                return l2;
            }
            if(l2 == null){
                return l1;
            }
            ListNode res = l1; //存储最终的结果
            ListNode pre = new ListNode(0); //用于因进位而产生的新的节点,在l1后new新的节点
            pre.next = l1;
            int carry = 0;
            while(l1 != null && l2 != null){
                l1.val = l1.val + l2.val + carry;
                carry = l1.val / 10;
                l1.val = l1.val % 10;
                pre = l1;
                l1 = l1.next;
                l2 = l2.next;
            }
            
            //l2比l1长,则l1直接指向l2剩余部分
            if(l2 != null){
                pre.next = l2;
                l1 = l2;
            }
            
            //实际为l2剩余部分
            while(l1 != null){
                l1.val = l1.val + carry;
                carry = l1.val / 10;
                l1.val = l1.val % 10;
                pre = l1;
                l1 = l1.next;
            }
            
            //最后还有进位
            if(carry > 0){
                pre.next = new ListNode(1);
            }
            
            return res;
            
            
        }

    我也在网上看了他人的代码,写的很简练。如下

    public class Solution {
        public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            return helper(l1,l2,0);
        }
    
        public ListNode helper(ListNode l1, ListNode l2, int carry){
            if(l1==null && l2==null){
                return carry == 0? null : new ListNode(carry);
            }
            if(l1==null && l2!=null){
                l1 = new ListNode(0);
            }
            if(l2==null && l1!=null){
                l2 = new ListNode(0);
            }
            int sum = l1.val + l2.val + carry;
            ListNode curr = new ListNode(sum % 10);
            curr.next = helper(l1.next, l2.next, sum / 10);
            return curr;
        }
    }
    public class Solution {
            public Long listTOLong(ListNode l){
            long num = 0;
            long temp =1;
            int i=0;
            while(l!=null){
                num = num+l.val*temp;
                temp=temp*10;
                l=l.next;
            }
            return num;
        }    
        public ListNode longToList(Long num){        
            ListNode l3 new ListNode(-1);
            l3.next null;
            ListNode c = l3;
            c.val=(int)(num%10);
            num = num/10;
            while(num>0){        
                ListNode cnext new ListNode((int)(num%10));
                cnext.next=null;
                c.next=cnext;
                num = num/10;
                c=c.next;
            }
            return l3;
        }
        public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            if(l1==null&&l2==null){
                return null;
            }
            //链表转long型
            long num1 = listTOLong(l1);
            long num2 = listTOLong(l2);
            //System.out.println("l1:"+num1+" l2:"+num2);
            long num3 = num1+num2;
            //System.out.println("l3:"+num3);
            //long型转链表
            ListNode l3 = longToList(num3);
            return l3;
            
        }
    }
  • 相关阅读:
    Sqlserver中的触发器
    Memcache在.Net中的使用
    Sqlserver中的储存过程
    Ado.NET基础必备
    MVC项目报错 ”基础提供程序在 Open 上失败”
    【Darwin学习笔记】之获取系统处理器数量的方法
    Wireshark抓包工具--TCP数据包seq ack等解读
    TCP:WireShark分析,序列号Seq和确认号Ack
    【Darwin学习笔记】之TaskThread
    boost::asio::io_context类
  • 原文地址:https://www.cnblogs.com/sMKing/p/6409596.html
Copyright © 2011-2022 走看看