The reason you run out of memory is this loop:
while(ptr1!=null)
{
Node nnode=new Node();
nnode.data=ptr1.data;
if(isEmpty())
{
start1=nnode;
}
else
{
nnode.link=start1;
start1=nnode;
}
}
It keeps creating new Node
s until ptr1
becomes null
, yet it never advances the ptr1
.
You need to either change the loop condition to check start1
, or change the body of the loop to move ptr1
to the next node.