java - Creating and chaining LinkedList in for loop -
i trying create new linkedlistnode
object parse through char[]
in reverse. want link these
char[] totalnum = (total + "").tochararray(); (int = totalnum.length; > 0; i--) { linkedlistnode sum = new linkedlistnode(totalnum[i]); sum.next = null; }
here linkedlistnode
class:
public class linkedlistnode { int data; public linkedlistnode next; // constructor public linkedlistnode(int newdata) { this.next = null; this.data = newdata; } }
i wondering how can go doing this. new java has gotten me stumped! know should create new linkedlistnode
object in each iteration far have gotten
public class linkedlistnode { int data; private linkedlistnode next; public linkedlistnode(int data) { this.data = data; } public void setnext(linkedlistnode next) { this.next = next; } public linkedlistnode getnext() { return next; } } char[] totalnum = (total + "").tochararray(); int length = totalnum.length; linkedlistnode tail /*or 'head', whatever suits you*/ = new linkedlistnode(totalnum[length - 1]); linkedlistnode newnode; linkedlistnode previousnode = tail; (int = length - 2; >= 0; i--) { newnode = new linkedlistnode(totalnum[i]); previousnode.setnext(newnode); previousnode = newnode; }
Comments
Post a Comment