In: Computer Science
pls use deep clone method to make a deep copy of each elements in a singly linked list.
(show detailed code and explanation in java. thanks!)
Program
//Node class
class Node
{
   //data members
   private int data;
   private Node link;
   //constructor
   public Node(int data, Node link){
      this.data = data;
   this.link = link;
   }
   //return the data
   public int getData(){
   return data;
   }
   //return the link
   public Node getLink(){
      return link;
   }
   //modified the data
   public void setData(int data){
   this.data = data;
   }
   //modified the link
   public void setLink(Node link){
   this.link = link;
   }
}
//singly linked list
class SLinkedList
{
   //head node
   private Node head;
  
   //constructor
   public SLinkedList(){
       head = null;
   }
  
   //method to insert an item to end of the list
   public void insert(int item)
   {
       //create a new node
       Node newnode = new Node(item,
null);
       //check list is empty
       if(head==null)
       {
           head =
newnode;
       }
       else
       {
           //traverse the
list
           Node tmp =
head;
          
while(tmp.getLink()!=null)
           {
          
    tmp = tmp.getLink();
           }
           //insert an item
to end of the list
          
tmp.setLink(newnode);
       }
   }
  
   //deep copy of each elements in a singly linked
list
   public static SLinkedList deepClone(SLinkedList
list1)
   {
       //create a new singly linked
list
       SLinkedList list2 = new
SLinkedList();
       //head1 refer to the head of the
original list
       Node head1 = list1.head;
       //create the head of the newly list
and head2 refer to the head
       Node head2 = list2.head = new
Node(head1.getData(), head1.getLink());
       //head1 refer to the next node of
the original list
       head1 = head1.getLink();
      
       //processing until end of the
list
       while(head1 != null)
       {
           //create a new
node
           Node newnode =
new Node(head1.getData(), head1.getLink());
           //advance one
node of the newly list
           head2 =
head2.getLink();
           //advance one
node of the original list
           head1 =
head1.getLink();
       }
       //return the newly list
       return list2;
   }
  
   //method to print the list
   public void printList()
   {
       if(head==null)
       {
          
System.out.println ("List is empty");
           return;
       }
       System.out.print("List: ");
       Node temp = head;
       while(temp!= null)
       {
           System.out.print
(temp.getData() + " ");
           temp =
temp.getLink();
       }
       System.out.println ();
   }
}
//LinkedListDemo class
class LinkedListDemo
{
   //main method
   public static void main (String[] args)
   {
       //create object of
SLinkedList
       SLinkedList list = new
SLinkedList();
      
       //Insert some values to the
list
       for(int i=1; i<=10; i++)
       {
          
list.insert(i);
       }
  
       //print the original list
       list.printList();
      
       //deep copy from original list to
newly cloned list
       SLinkedList list2 =
SLinkedList.deepClone(list);
      
       //print the newly cloned list
       list2.printList();
   }
}
Output:
List: 1 2 3 4 5 6 7 8 9 10
List: 1 2 3 4 5 6 7 8 9 10
Solving your question and
helping you to well understand it is my focus. So if you face any
difficulties regarding this please let me know through the
comments. I will try my best to assist you.
Thank you.