码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2018-05-16 17:22:35      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:ram   pac   nod   ber   next   boolean   must   int   style   

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);
    }

}

 

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

标签:ram   pac   nod   ber   next   boolean   must   int   style   

原文地址:https://www.cnblogs.com/lowseasonwind/p/9046843.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!