Single linked list implements two sets of integer additions (Java)

Source: Internet
Author: User
Tags assert

 Packageorg.skyeye.test;ImportOrg.springframework.util.Assert; Public classNodeadd { Public Static classnode{intdata;                Node Next;  Public Static enumnumber{Zero (0), one (1), both (2), three (3), four (4), five (5), six (6), seven (7), eight (8), nine (9); intvalue; Number (intvalue) {                 This. Value =value; }                         Public Static BooleanInintvalue) {                 for(number v:number.values ()) {if(V.value = =value) {                        return true; }                }                return false; }                                }                 PublicNode (intdata, Node next) {Assert.state (number.in (data),"Must is in 0-9.");  This. data =data;  This. Next =Next; }                 Public voidSetData (intdata) {             This. data =data; }                 Public voidSetnext (Node next) { This. Next =Next; }    }             Public Staticnode Reverse (node head) {if(head==NULL|| head.next==NULL) {            returnHead; } Node Pre=NULL; Node Current=Head;  while(Current! =NULL) {Node tmp=Current ; Current=Current.next;            Tmp.setnext (pre); Pre=tmp; }                returnPre; }         Public Staticnode Add (node One, node) {inti = 0; Node one1=reverse (one); Node TWO1=reverse (both); Node Pre=NULL;  while(one1!=NULL|| two1!=NULL) {            intTMP =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=NewNode (TMP,NULL); T.next=Pre; Pre=T; }                if(i>0) {Node head=NewNode (i, pre); Pre=Head; }                returnPre; }             Public Static voidprint (Node head) {StringBuilder builder=NewStringBuilder ();  while(head!=NULL) {builder.append (Head.data). Append (","); Head=Head.next;    } System.out.println (Builder.tostring ()); }         Public Static voidMain (string[] args) {Node one=NewNode (1,NewNode (2,NewNode (3,NewNode (4,NewNode (5,NULL))))); Node=NewNode (3,NewNode (4,NewNode (5,NULL))); Node result=Add (one, both);    print (result); }}

Single linked list implements two sets of integer additions (Java)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.