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)