Advertisement
Guest User

jbdsb usnvpnod

a guest
Aug 1st, 2015
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.78 KB | None | 0 0
  1.     public void clone(LinkedList lst, LinkedList newlst){
  2.        
  3.      
  4.             Node oldlisthead = lst.head; //  reference to oldlst head
  5.         Node newlisthead = newlst.head; // reference to newlst head
  6.         Node currentold = lst.head;// reference done to be used in loop
  7.        
  8.         newlst.head = new Node(lst.head.data); // creating head node for newlst
  9.         currentold = currentold.next;  
  10.        
  11.         Node currentnew = newlst.head; // reference to head node of newlst
  12.        
  13.         while(currentold != null){     
  14.             Node temp = new Node(currentold.data);
  15.             if(newlst.head.next == null){
  16.                 newlst.head.next = temp;
  17.                 currentnew = currentnew.next;
  18.                 currentold = currentold.next;
  19.             }
  20.             else{
  21.                 currentnew.next = temp;
  22.                 currentnew = currentnew.next;
  23.                 currentold = currentold.next;
  24.             }          
  25.         }
  26.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement