Understandable Java Solution Using Traverse


  • 0
    L
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            int onelength = obtainListLength(l1);
            int twolength = obtainListLength(l2);
            int maxlength = 0;//最大数位
            ListNode listone = l1;
            ListNode listtwo = l2;
            if(onelength>=twolength){
                maxlength = onelength;
                ListNode tmpNode = l2;
                for(int i=0;i<onelength-twolength;i++){
                    ListNode tmpNode2 = tmpNode;
                    tmpNode = new ListNode(0);
                    tmpNode.next = tmpNode2;
                }
                listtwo = tmpNode;
            }else {
                maxlength = twolength;
                ListNode tmpNode = l1;
                for(int i=0;i<twolength-onelength;i++){
                    ListNode tmpNode1 = tmpNode;
                    tmpNode = new ListNode(0);
                    tmpNode.next = tmpNode1;
                }
                listone = tmpNode;
            }
            int[] signalArray = new int[maxlength+1];
            int[] valueArray = new int[maxlength+1];
            signalArray[maxlength] = 0;
            for(int i=1;i<=maxlength;i++){
                int value = listone.val + listtwo.val;
                if(value >= 10){
                    valueArray[i] = value%10;
                    signalArray[i-1] = 1;
                }else {
                    valueArray[i] = value;
                    signalArray[i-1] = 0;
                }
                listone = listone.next;
                listtwo = listtwo.next;
            }
            for(int i=maxlength;i>=0;i--){
                int value = valueArray[i]+signalArray[i];
                if(value>=10){
                    valueArray[i] = value%10;
                    signalArray[i-1] = signalArray[i-1]+1;
                }else {
                    valueArray[i] = value;
                }
            }
            ListNode head = null;
            ListNode current = null;
            for(int i=0;i<=maxlength;i++){
                if(i==0&&valueArray[i] == 0){
                    continue;
                }
                if(i==0&&valueArray[i] != 0){
                    head = new ListNode(valueArray[0]);
                    current = head;
                }
                if(i==1&&valueArray[0] == 0){
                    head = new ListNode(valueArray[1]);
                    current = head;
                }
                if(i==1&&valueArray[0] != 0){
                    current.next = new ListNode(valueArray[1]);
                    current = current.next;
                }
                if(i>=2){
                    current.next = new ListNode(valueArray[i]);
                    current = current.next;
                }
            }
            return head;
        }
        
        private int obtainListLength(ListNode listNode){
            ListNode head = listNode;
            int counter = 0;
            while(head!=null){
                counter++;
                head = head.next;
            }
            return counter;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.