Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

JavaWorld Daily Brew

Linked List Help



I have this linked list:

public class LinkedList
{
  public static ListNode start=null;
 
  public LinkedList()
  {
  } 
 
  public void insert(String x)
  {
    ListNode current;
    current=start;
    ListNode node = new ListNode();
    node.setData(x);
    if (start==null)
    {
      start=node;
      node.setNext(null);
    }
    else
    {
      while (current.getNext()!=null)
        current=current.getNext();
      node.setNext(null);
      current.setNext(node);
    }
    //System.out.println("inserting "+node.getData());
  }

  public void printList()
  {
      ListNode current;
      current=start;
      while (current!=null)
      {
        System.out.print(current.getData()+" ");
        current=current.getNext();
      }
      System.out.println();
  }
}

And I'm running a basic LinkedListTest:

     LinkedList list1 = new LinkedList();
     LinkedList list2 = new LinkedList();
     list1.insert(23);
     list2.insert(6665);
     list1.printList();
     list2.printList();
   

And for some reason, the output is:
23.0 6665.0
23.0 6665.0

If I comment out the printing of list2, the output is still 23.0 6665.0.
Somehow, the data in list2 is getting into list1.
I haven't a clue why.

Thank you so much in advance for any help.