Adding to Linked Lists

I have a problem with implementing the following:
Polygon p = new Polygon();
Polygon p2 = new Polygon();
p.setNext( p2 );
Polygon p3 = new Polygon();
Polygon p4 = new Polygon();
p3.setNext( p4);
Polygon p5 = new Polygon();
p4.setNext( p5);
//Now add the second list to the first one
p.setNext( p3 );
int i = 0;
while( p.next != null)
{
i++;
p = p.next;
}
System.out.println( i );
It seems that after inserting p3 the list stops.
Here is the code for the setNext method:
public void setNext( Polygon pe )
{

    if( pe == null)
    {
       // next = null;
        return;
    }
    Polygon cur = next;
    Polygon prev = next;

    while ( cur != null )
    {
        prev = cur;
        cur = cur.next;
    }

    pe.next = cur;


    if ( cur == next )
    {
        next = pe;
    } else
    {
        prev.next = pe;
    }
}

I think there should be the problem but only i don’t see it :frowning:

thanks in advance

Why aren’t you using the LinkedList class?

It would also help if you state what you think that code is supposed to do. Are you trying to insert one list within another, or append one list to another?

Are you sure you want to set pe.next? Or did you mean to set the next of the last node in the pe list?

Anyway - as Blah^3 said, use java.util. See how much time you have wasted already coding a list yourself :slight_smile: ?