[leetcode NO.3] Add Two Numbers (JAVA)

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

这个题目的方法很多 1. 可以直接向binary add一样, 用一个while循环,然后在加上没到尾的那个链表

  2.下面我给出递归的方法, 比较简洁, 算是提供一个思路吧

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    int c = 0;//因为函数名及参数限定了, 所以我在外面来写个进位carry
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int s = 0;//用来算和sum
        if(l1 == null && l2 == null && c == 0)//两个都遍历完了,并且进位为0,就可以全部返回了
            return null;
        else if (l1 == null && l2 == null && c != 0){//两个都遍历完了,进位不为0时,需要继续new一个node给c
            s = c;
            c = 0;
        }
        else if(l1 == null && l2 != null){     // L1到尾了, 以后L1提供0,并且不next了
            s = (l2.val + c)%10;
            c = (l2.val + c)/10;
            l2 = l2.next;
        }
        else if(l2 == null && l1 != null)// L2到尾了, 以后L2提供0,并且不next了
            s = (l1.val + c)%10;
            c = (l1.val + c)/10;
            l1 = l1.next;
        }
        else// 都没到尾, 都要next
            s = (l1.val + l2.val + c)%10;
            c = (l1.val + l2.val + c)/10;
            l1 = l1.next;
            l2 = l2.next;
        }
        ListNode node = new ListNode(s);
        node.next = addTwoNumbers(l1, l2);
        return node;
    }
}

扩展: 如果要求反着来, 链表的末尾先加,直到链表头, 这样就有点意思了!

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

Output: 8-> 0 -> 7

首先要注意:2->3 和 5->6->7的情况是要023+567, 所以我们要将短的用头插法插入0


由于java中没有指针,所以我们建立一个class来辅助实现

public class Solution {
	public class ListNode {
	      int val;
	      ListNode next;
	      ListNode(int x) {
	          val = x;
	          next = null;
	      }
	  }
	public class Sumhead{
		ListNode sumnext; 
		int carry = 0;
	}
	
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        if(l1.length() < l2.length())           //将两个list设为等长,头插法并用0补充。
            headinsertHelp(l1,l2.length()-l1.length());
        if(l1.length() > l2.length()) 
            headinsertHelp(l2,l1.length()-l2.length());
        Sumhead  sd = addhelper(l1,l2);//sd为辅助class
        if(sd.carry == 0)           //退出循环时候有可能carry不为0,需要为他建立新节点
            return sd.sumnext;
        else{
            ListNode result = headinsert(sd.sumnext, sd.carry);     //只是在sumnext前面插入一个新节点即可
            return result;
        }
    }
    
    public Sumhead addhelper(ListNode *l1, ListNode *l2){
        if(l1 == null && l2 == null){//到底时候,新建sumhead
            Sumhead sd = new Sumhead();
            sd.carry = 0;
            sd.next == null;
            return sd;
        }
        addhelper(l1.next, l2.next);//因为是后插,所以操作都在递归后面
        int s = (l1.val + l2.val + sd.carry);
        ListNode res = new ListNode(s%10);
        sd.sumnext = res;//sd 是一个头,;后面的数都在他后面
        sd.carry = s/10;
        retrun sd;
    }
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值