-8

So I have some Set, and I want to add the elements to a LinkedList so I can iterate over them. How would I go about doing this in java?

2
  • 2
    with code. with code. OR an internet search and then code. Commented Oct 19, 2013 at 2:56
  • puslic static void main(String[] args) Commented Oct 19, 2013 at 3:22

1 Answer 1

0

Taken from a question here: Adding items to end of linked list

class Node {
    Object data;
    Node next;
    Node(Object d,Node n) {
        data = d ;
        next = n ;
       }

   public static Node addLast(Node header, Object x) {
       // save the reference to the header so we can return it.
       Node ret = header;
   // check base case, header is null.
   if (header == null) {
       return new Node(x, null);
   }

   // loop until we find the end of the list
   while ((header.next != null)) {
       header = header.next;
   }

   // set the new node to the Object x, next will be null.
   header.next = new Node(x, null);
   return ret;
   }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.