Copy SLL Examples
Copy SLL Examples
public SingleLinkedList(SingleLinkedList<E>l1){
if (l1.head==null){head=null;size=0; return;}
Node<E> ptr=l1.head;
Node<E> nptr=new Node<E>(ptr.data);
head=nptr;
ptr=ptr.next;
while (ptr!=null) {
Node<E> temp=new Node<E>(ptr.data);
nptr.next=temp;
nptr=temp;
ptr=ptr.next; } //end of while
size= l1.size();
}
** copy constructor ( copy alternate) copy node and skip the next
one
public SingleLinkedList(SingleLinkedList<E>l1){
if (l1.head==null){head=null;size=0; return;}
Node<E> ptr=l1.head;
Node<E> nptr=new Node<E>(ptr.data);
head=nptr;
size++;
ptr=ptr.next;
if (ptr!=null)
ptr=ptr.next;
while (ptr!=null){
Node<E> temp=new Node<E>(ptr.data);
nptr.next=temp;
nptr=temp;
size++;
ptr=ptr.next;
if (ptr!=null)
ptr=ptr.next;
}