美文网首页
Add Two Numbers

Add Two Numbers

作者: 看风景的人_21744 | 来源:发表于2017-09-14 11:14 被阅读0次
    • 问题
      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

    • 解决

    package com.lsy.leetcode;
    
    public class AddTwoNumbers {
    
         static AddTwoNumbers a=new AddTwoNumbers();
         public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
    //       if (l1 == null) {       //说了非空
    //              return l2;
    //          }
    //
    //          if (l2 == null) {
    //              return l1;
    //          }
                int carry = 0;
                
                ListNode newhead = a.new ListNode(-1);
                ListNode l3 = newhead;
                //ListNode l3;
                while(l1!=null || l2!=null){
                      if(l1!=null){
                          carry += l1.val;
                         // System.out.println(l1.val+"test");
                          l1 = l1.next;
                      }
                      if(l2!=null){
                          carry += l2.val;
                          l2 = l2.next;
                      }
                      //l3.next = a.new ListNode(carry%10);  //测试不能从l3开始也可以的
                      l3.val=(carry%10);//那就不是指向newhead了,需要加一个改变val的方法
                      System.out.println(newhead.val);
                      carry = carry/10;
                      l3.next=a.new ListNode(-1);//用之前实例化l3;
                      l3=l3.next;   //不能和上一行调换位置
                }
                
                if(carry == 1)
                     l3.next=a.new ListNode(1);
              
               // return newhead.next;
               // System.out.println(newhead.val+"->"+newhead.next.val+"->"+newhead.next.next.val);
                return newhead; 
            }
                
        
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            
            ListNode l1=a.new ListNode(2);  //内部类,不懂呢
            l1.next=a.new ListNode(4);
            l1.next.next=a.new ListNode(3);
            
            ListNode l2=a.new ListNode(5);  //内部类,不懂呢
            l2.next=a.new ListNode(6);
            l2.next.next=a.new ListNode(4);
            //l1.next=new ListNode(4);
            
            ListNode l3=addTwoNumbers(l1,l2);
            System.out.println(l3.val+"->"+l3.next.val+"->"+l3.next.next.val);
            System.out.println(l1.val);  //测试引用传递,l1没有变。传递的是引用的复制。
                                         //参考:http://www.cnblogs.com/lixiaolun/p/4311863.html
        }
    
         class ListNode {
                  int val;
                  ListNode next;
                  public ListNode(int x) { val = x; }
              }
         
         public ListNode addTwoNumbers2(ListNode l1, ListNode l2) {
                ListNode dummyHead = new ListNode(0);
                ListNode p = l1, q = l2, curr = dummyHead;
                int carry = 0;
                while (p != null || q != null) {
                    int x = (p != null) ? p.val : 0;   //先确定下来
                    int y = (q != null) ? q.val : 0;
                    int sum = carry + x + y;  
                    carry = sum / 10;              //取整
                    curr.next = new ListNode(sum % 10);  //取余
                    curr = curr.next;
                    if (p != null) p = p.next;
                    if (q != null) q = q.next;
                }
                if (carry > 0) {
                    curr.next = new ListNode(carry);
                }
                return dummyHead.next;
            }
    }
    
    

    相关文章

      网友评论

          本文标题:Add Two Numbers

          本文链接:https://www.haomeiwen.com/subject/gnmrsxtx.html