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 8783587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:51:27+00:00 2026-06-13T20:51:27+00:00

This is an homework assignment. To change the following recursive deep copy method into

  • 0

This is an homework assignment. To change the following recursive deep copy method into an iterative equivalent. I came up close, and need your help to make it right.
Recursive implementation:

public static StringNode copy(StringNode str) {
        if (str == null)
            return null;

        StringNode copyFirst = new StringNode(str.ch, null);
        copyFirst.next = copy(str.next);
        return copyFirst;
    }

Here is what I came up, the iterative equivalent. The static length() method has been implemented already to return how many nodes are there in a given link list.

public static StringNode copy(StringNode str) {
    if (str == null)
        return null;

    StringNode firstNode = new StringNode(str.ch ,null);
    StringNode prevNode = firstNode;
    StringNode nextNode;

    for (int i = 1; i < length(str); i++) {
        nextNode = new StringNode(str.next.ch, null);
        prevNode.next = nextNode;
        prevNode = nextNode;
    }

    return firstNode;
}

The problem: to test my implementation, I create a linked list str1 with character value, 'n', 'b', 'a', then call

StringNode copy = StringNode.copy(str1);

then I delete the last node of str1, leave it as 'n','b',
however, when i try to print out the content stored in copy, I get
'n', 'b', 'b' instead of 'n', 'b', 'a'.

Any suggestions?

  • 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-06-13T20:51:28+00:00Added an answer on June 13, 2026 at 8:51 pm

    You also need to move the str forward in your loop, else you are continuously adding the same str in your list in every iteration. First element is different for the first time invocation of method. and then str.next is same through out your loop.

    So, you need to add this code in your for loop: –

    str = str.next;
    

    Also, your loop has some problem. You should not iterate till the length(str). But till str == null.

    So, finally your loop should look like this: –

    while (str.next != null) {  // Iterate till str.next != null, as we are creating 
                                // the next node in the loop for current node str
    
        nextNode = new StringNode(str.next.ch, null);
        prevNode.next = nextNode;
        prevNode = nextNode;
    
        str = str.next;  // Move to next node.
    }
    

    A while loop has to be used in this case, since you don’t know how many times the loop should iterate.

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

Sidebar

Related Questions

Let me start by saying this is a homework assignment, I don't need any
This is for a homework assignment. I am supposed to change the contents of
I need to do this homework assignment using prolog (SWI-flavor) and cant get my
This is a homework assignment. I just need a nudge. I'm trying to create
I am working on this homework assignment and I am stuck on what I
First of all, this is a homework assignment. I'm trying to make some bubbleSort
This is part of a homework assignment. I've got several questions asking find the
I know this sounds like a homework assignment, but it isn't. Lately I've been
(this is indirectly a part of a much larger homework assignment) I have something
Full disclosure : This is for a homework assignment. This is driving me nuts.

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.