leetCode 2 Add Two Numbers

问题:Add Two Numbers

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


解题过程: 在解题的过程中首先因为是数的反序,故先将链表反转,然后相加,同时比较链表长度,如不一致,则需表头补0。

不过后面提交的过程中,发现完全不需要反转链表,主要是243+564->708,狗日的342+465->708是相同的,造成要反转。


解题代码:


/**
 * 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) {
       //ListNode pre1 = l1,cur = l1, next = null,pre2 = l2;
       //int i = 0,j = 0;
       //艹,发现想多了,返回数据根本不需要反转
       
       //l1翻转
//        while(cur != null){
//            next = cur;
//            cur = cur.next;
//            next.next = pre1;
//            pre1 = next;
//            i++;//记录l1位数
//        }
//        l1.next = null;
//        
//        //l2反转
//        cur = l2;
//        while(cur != null){
//            next = cur;
//            cur = cur.next;
//            next.next = pre2;
//            pre2 = next;
//            j++;//记录l2位数
//        }
//        l2.next = null;
       //System.out.println("i:" + i);
       //System.out.println("j:" + j);
       //如果l1大于l2位数,则l2头结点补0;反之亦然
//        if(i > j){
//         for(int k = 0; k < i-j; k++){
//         ListNode ln = new ListNode(0); 
//         ln.next = pre2;
//         pre2 = ln;
//        }
//        }
//        else{
//         for(int k = 0; k < j-i; k++){
//         ListNode ln = new ListNode(0); 
//         ln.next = pre1;
//         pre1 = ln;
//        }
//        }
       
       //两数相加
       int addNum = 0;
       ListNode last = null;
       ListNode head = null;
       boolean isOver = false;//是否进位
       while(l1 != null && l2 != null){
        if(isOver){
        addNum = l1.val + l2.val + 1;
        isOver = false;
        }
        else{
        addNum = l1.val + l2.val;
        }
        if(addNum > 9){
        isOver = true;
        addNum = addNum % 10;//取个位数
        }
        ListNode ln = new ListNode(addNum); 
        if(head == null){
        last = head = ln;
        }else{
        last.next = ln;
        last = ln;
        }
        l1 = l1.next;
        l2 = l2.next;
       }
       
       //如果l1,l2位数不相同,则必有且只有1个还有数据
       while(l1 != null){
        //如果有进位
        if(isOver){
        l1.val += 1;
        isOver = false;
        if(l1.val > 9){
        isOver = true;
        l1.val = l1.val % 10;//取个位数
        }
        }
        last.next = l1;
        last = l1;
        l1 = l1.next;
       }
       while(l2 != null){
        //如果有进位
        if(isOver){
        l2.val += 1;
        isOver = false;
        if(l2.val > 9){
        isOver = true;
        l2.val = l2.val % 10;//取个位数
        }
        }
        last.next = l2;
        last = l2;
        l2 = l2.next;
       }
       //如果还有进位未处理
       if(isOver){
        ListNode ln = new ListNode(1); 
        last.next = ln;
        last = ln;
       }
       
       //如果位数不相等,则把剩余的相加最后
      // while(head != null)
      // {
     //  System.out.println(head.val);
     //  head = head.next;
      // }
return head;
   }
}



上面的代码用了递归排序的思想,不过实现太罗嗦了,下面为简洁的实现方式:


/**
 * 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) {
       //两数相加
       int addNum = 0;
       ListNode last = new ListNode(0);
       ListNode head = last;
       int isOver = 0;//是否进位
       while(true){
        int v1 = l1 == null ? 0 : l1.val;
        int v2 = l2 == null ? 0 : l2.val;
        
        addNum = v1 + v2 + isOver;
        isOver = addNum / 10;
        addNum = addNum % 10;
        
        last.val = addNum;
        
        if(l1 != null)
        <span style="white-space:pre">	</span>l1 = l1.next;
        if(l2 != null)
        <span style="white-space:pre">	</span>l2 = l2.next;
        
        if(l1 == null && l2 == null && isOver == 0){
        <span style="white-space:pre">	</span>break;
        }
        else{
        <span style="white-space:pre">	</span>last.next = new ListNode(0);
        <span style="white-space:pre">	</span>last = last.next;
        }
       }
return head;
   }
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值