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

Posted 淡季的风

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了单链表实现两组整数相加(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);
    }

}

 

以上是关于单链表实现两组整数相加(Java)的主要内容,如果未能解决你的问题,请参考以下文章

单链表实现大数相加

数据结构 链表

超硬核十万字!全网最全 数据结构 代码,随便秒杀老师/面试官,我说的

用JAVA线性表来编写“任意大的整数相加减”

链表常见的题型(java实现)

一元多项式的相加(单链表的应用)