单链表实现两组整数相加(Java)

package org.skyeye.test;

import org.springframework.util.Assert;

public class NodeAdd {
    
    public static class Node{
        int data;
        Node next;
        
        public static enum Number{
            zero(0),
            one(1),
            two(2),
            three(3),
            four(4),
            five(5),
            six(6),
            seven(7),
            eight(8),
            nine(9);
                    
            int value;
            
            Number(int value) {
                this.value = value;
            }
            
            public static boolean in(int value) {
                for(Number v: Number.values()) {
                    if(v.value == value) {
                        return true;
                    }
                }
                return false;
            }
            
            
        }
        
        public Node(int data, Node next) {
            Assert.state(Number.in(data), "must be in 0-9.");
            this.data = data;
            this.next = next;
        }
        
        public void setData(int data) {
            this.data = data;
        }
        
        public void setNext(Node next) {
            this.next = next;
        }
    }
    
    
    public static Node reverse(Node head) {
        if(head==null || head.next==null) {
            return head;
        }
        
        Node pre = null;
        Node current = head;
        while(current != null) {
            Node tmp = current;
            current = current.next;
            tmp.setNext(pre);
            pre = tmp;
        }
        
        return pre;
    }
    
    public static Node add(Node one, Node two) {
        int i = 0;
        
        Node one1 = reverse(one);
        Node two1 = reverse(two);
        
        Node pre = null;
        while(one1!=null || two1!=null) {
            int tmp = i;
            
            if(one1 != null) {
                tmp += one1.data;
                one1 = one1.next;
            }
            
            if(two1 != null) {
                tmp += two1.data;
                two1 = two1.next;
            }
            
            if(tmp>=10) {
                tmp -= 10;
                i = 1;
            }else {
                i = 0;
            }
            
            Node t = new Node(tmp, null);
            t.next = pre;
            pre = t;
        }
        
        if(i>0) {
            Node head = new Node(i, pre);
            pre = head;
        }
        
        return pre;
    }
    
    
    public static void print(Node head) {
        StringBuilder builder = new StringBuilder();
        while(head!=null) {
            builder.append(head.data).append("->");
            head = head.next;
        }
        System.out.println(builder.toString());
    }
    
    public static void main(String[] args) {
        Node one = new Node(1, new Node(2, new Node(3, new Node(4, new Node(5, null)))));
        Node two = new Node(3, new Node(4, new Node(5, null)));
        Node result = add(one, two);
        print(result);
    }

}

猜你喜欢

转载自www.cnblogs.com/lowseasonwind/p/9046843.html