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

  • SEARCH
  • Home
  • 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 8597507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:55:44+00:00 2026-06-12T00:55:44+00:00

For example I make the recursive call defined below. The method finds the kth

  • 0

For example I make the recursive call defined below. The method finds the kth element from the last. If found, it assigns the current node to the object I pass to the recursive call. For some reason the node kth is null. Can you not do things this way? And why?

public void findKthFromLast(Node head, int k){
    Node kth;     
    recrusiveHelper(head, k, kth);
    System.out.println(kth.data); //this is null
}

public int recursiveHelper(Node n, int k, Node kthFromLast){
    (if n == null){
        return 0;
    }
    val = 1 + recursiveHelper(n.next, k, kthFromlast);
    if(k == val){
        kthFromLast = n;
    }
    return val;
}
  • 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-12T00:55:46+00:00Added an answer on June 12, 2026 at 12:55 am

    Firstly, that code shouldn’t compile because kth is not initialized and so cannot be used as argument for the recursiveHelper method call.

    Secondly, any changes to the references in a called method are not propagated to the caller in Java, i.e.

    private void caller()
    {
        StringBuilder s = new StringBuilder();
        s.append("test");
        calledMethod1(s);
        System.out.println(s.toString());
        calledMethod2(s);
        System.out.println(s.toString());
    }
    
    private void calledMethod1(StringBuilder buffer)
    {
        buffer = new StringBuilder();
        buffer.append("calledMethod1");
        return;
    }
    
    private void calledMethod2(StringBuilder buffer)
    {
        buffer.append(", calledMethod2");
        return;
    }
    

    Output:

    test
    test, calledMethod2
    

    The reason is, in calledMethod1 you are merely changing what buffer reference points to but not making any changes to what buffer reference was pointing when the method was called. In calledMethod2, you are making changes to the object referred by buffer and hence the changes are visible in the caller.

    If you are someone coming from C or C++ background, this is equivalent to assigning to a pointer argument in a called method which doesn’t affect what was passed in the caller.

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

Sidebar

Related Questions

JSFIDDLE EXAMPLE BELOW I want to make the following CSS layout and currently im
hello i am make like this example which i get from www.xxxxxx.com http://s1172.photobucket.com/albums/r568/novikoraharja/?action=view&current=justanimage.png i
I have a simple recursive method, a depth first search. On each call, it
The last day i've tried to make this work: Wake up my computer from
I tried following the following example to make an HTTP link in my TextView
For example I have make test # do other stuff 'make test' runs some
I don't understand Access Modifiers in OOP. Why do we make for example in
How can I make this sliding panel example to slide down on mouseover and
I want to make a time or date listener which for example invoke an
For example, I know I can make a new row and enter values into

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.