I have a constructor public LinkedList(int[]) that will read primitive values in an array and place them in a linked list. How do I go about this?
help with linked lists
Collapse
X
-
You came up with the constructor, and yet you don't know how to use it?Originally posted by yolkmanI have a constructor public LinkedList(int[]) that will read primitive values in an array and place them in a linked list. How do I go about this?
And your code will actually create a linked list of arrays, a little different than moving values from an array into a linked list...
A googlesearch of 'java linked list'
http://java.sun.com/j2se/1.4.2/docs/...inkedList.html -
Is this a user defined LinkedList then?
All you need to then is to assign the array values as elements of the linked list.
Depending on how you implemeted the ListNode,
Use a for that goes through the array. At each step take two elemets out,
create two nodes and do something like nodei.next = node(i+1). So the node created with element at i + 1 is assigned the next of the node created with element at i. Of course you should set the first elent accordinglyComment
Comment