Leetcode:Add Two Numbers分析和實現

Add Two Numbers這個問題的意思是,提供兩條鏈表,每條鏈表表示一個十進制整數,其每一位對應鏈表的一個結點。好比345表示爲鏈表5->4->3。而咱們須要作的就是將兩條鏈表表明的整數加起來,並建立一個新的鏈表並返回,新鏈表表明加總值。java


 這個問題與算法無關,其主要是操做鏈表這一數據結構,以及兩個大數的加法。因爲沒有規定鏈表的長度,這意味着鏈表表明的整數能夠任意大,這樣就不能先利用鏈表計算出對應的整數值,以後利用加總值從新生成鏈表。咱們必須手動處理兩個大數的加法。算法

要謹慎兩個個位數的加總結果是有可能須要進位的,即7+8實際上等於10x1+5,其中1應該進位到下一個結點,而5看成這個當前結點的值。數據結構

addTwoNumbers(Node n1, Node n2)app

  res = Node{val = (n1.val + n2.val) % 10, next = null}ide

  tail = resspa

  adv = (n1.val + n2.val) / 10code

  n1 = n1.nextblog

  n2 = n2.nextleetcode

  while n1 != null && n2 != null thenget

    sum = n1.val + n2.val + adv

    tail.next = Node{val = sum % 10, next = null}

    tail = tail.next

    adv = sum / 10

    n1 = n1.next

    n2 = n2.next

  Node notEndedNode = (n1 == null ? n2 : n1)

  while notEndedNode != null then

    sum = notEndedNode .val + adv

    tail.next = Node{val = sum % 10, next = null}

    tail = tail.next

    adv = sum / 10

    notEndedNode = notEndedNode.next

  while adv != 0 then

    tail.next = Node{val = adv % 10, next = null}

    tail = tail.next

    adv = adv / 10

  return res


 下面直接給出java的實現代碼

package cn.dalt.leetcode;


/**
 * Created by Administrator on 2017/6/4.
 */
public class AddTwoNumbers {

    public static void main(String[] args)
    {
        System.out.println(new AddTwoNumbers().addTwoNumbers(new ListNode(342), new ListNode(465)));
    }

    static class ListNode {
        ListNode(int x)
        {
            val = x % 10;
            int adv = x / 10;
            if(adv != 0)
            {
                next = new ListNode(adv);
            }
        }

        @Override
        public String toString() {
            return "" + val + (next != null ? "->" + next.toString() : "");
        }

        int val;
        ListNode next;
    }

    static class NodeList{
        ListNode tail;
        ListNode root;
        int advance = 0;
        public NodeList(int x)
        {
            root = new ListNode(x % 10);
            tail = root;
            advance = x / 10;
        }
        public void append(int x)
        {
            x += advance;
            ListNode temp = new ListNode(x % 10);
            advance = x / 10;

            tail.next = temp;
            tail = temp;
        }
        public ListNode getResult()
        {
            while(advance != 0)
            {
                append(0);
            }
            return root;
        }

        @Override
        public String toString() {
            return root.toString();
        }
    }

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        NodeList list = new NodeList(l1.val + l2.val);
        l1 = l1.next;
        l2 = l2.next;

        while(l1 != null && l2 != null)
        {
            list.append(l1.val + l2.val);
            l1 = l1.next;
            l2 = l2.next;
        }
        while(l1 != null)
        {
            list.append(l1.val);
            l1 = l1.next;
        }
        while(l2 != null)
        {
            list.append(l2.val);
            l2 = l2.next;
        }
        return list.getResult();
    }
}
相關文章
相關標籤/搜索