Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3680410
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:34:24+00:00 2026-05-19T03:34:24+00:00

I have a linear linked list that consists of nodes: class Node{ Object data;

  • 0

I have a linear linked list that consists of nodes:

class Node{
    Object data;
    Node link;

    public Node(Object pData, Node pLink){
        this.data = pData;
        this.link = pLink;
    }

    public String toString(){
        if(this.link != null){
            return this.data.toString() + this.link.toString();
        }else{
            return this.data.toString() ;
        }
    }

    public void inc(){
        this.data = new Integer((Integer)this.data + 1);
    }

    public Node copy(){
        return new Node(this.data, this.link.copy());
    }
}

I want to make a deep copy of a list. Then increment each node of the original list and print both. I do not know if the code is right.

class Aufg1{
    public static void main(String args[]){
        Node node3 = new Node(new Integer(3), null);            
        Node node2 = new Node(new Integer(2), node3);           
        Node node1 = new Node(new Integer(1), node2);
        System.out.println(node1.copy().toString());
        System.out.println(node1.toString());
    }
}

…gives me just 123 for the second println but something is wrong with the copy. Any ideas?

Update:

   public Node copy(){
        if(this.link != null){
            return new Node(new Integer((Integer)this.data), this.link.copy());
        }else{
            return new Node(new Integer((Integer)this.data), null);
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T03:34:25+00:00Added an answer on May 19, 2026 at 3:34 am

    A deep copy would imply that you need to do a copy of data also.

     public Node copy(){
        return new Node(copy of this.data, this.link.copy());
     }
    

    So you need to decide how you are going to copy the object.

    EDIT: You can use something like the Deep Cloning Library to help. They use reflection etc. If you know the type of objects in that you are creating the class of objects yourself you can create a copy constructor.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have completed implementing Operation of Linear Linked List using C , Now inorder
I'm writing a simple linear linked list implementation in PHP. This is basically just
I have a linked list of around 5000 entries (NOT inserted simultaneously), and I
I have a linear layout that contains 6 images, which span the linear layout
I have a layout that is three linear layouts. The top has some icons
Currently, I have the following class: #btnHelp { background: url(images/btnHelp.png) center no-repeat, -webkit-gradient(linear, left
I have a piece of code that filters a list using LINQ, creates a
Say have a linear model LM that I want a qq plot of the
I have a linear layout which consists of imageview and textview , one below
I'm trying to figure out the best practice to setup hierarchy data that i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.