Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void clone(LinkedList lst, LinkedList newlst){
- Node oldlisthead = lst.head; // reference to oldlst head
- Node newlisthead = newlst.head; // reference to newlst head
- Node currentold = lst.head;// reference done to be used in loop
- newlst.head = new Node(lst.head.data); // creating head node for newlst
- currentold = currentold.next;
- Node currentnew = newlst.head; // reference to head node of newlst
- while(currentold != null){
- Node temp = new Node(currentold.data);
- if(newlst.head.next == null){
- newlst.head.next = temp;
- currentnew = currentnew.next;
- currentold = currentold.next;
- }
- else{
- currentnew.next = temp;
- currentnew = currentnew.next;
- currentold = currentold.next;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement