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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:00:07+00:00 2026-05-26T19:00:07+00:00

I know this question has been asked already but I would like to solve

  • 0

I know this question has been asked already but I would like to solve it using recursion. Each node in the list contains an integer value (int val). I would like to return the integer value, not just print it. I came up with the following:

int findKthNode (node* head, int n){
    if(!head)
        return 1;
    int retval = findkthNode(head->next, n);
    if(retval==n)
        return head->val;
    return 1+retval;
}

Once I reach the end of the list, I return 1. After that I add 1 to the previous return value until I reach the nth node from the end. At that point I return the int value at that node. There is one problem with this approach. I continue to add 1 until I return back to the first call. So if my list was 1,5,10,20,40,80,100, I would end up returning 85 for n=2, instead of 80 since I would add 1 five more times before returning. How would I fix this?

Also, I’m not sure if this is possible, but is there a way to return a pointer to the nth last node using recursion. I can’t see a way to do this with a singly linked list.

  • 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-26T19:00:07+00:00Added an answer on May 26, 2026 at 7:00 pm

    Don’t use the same variable for two different things, use two different variables. Pass an integer by reference to keep track of how many nodes from the end you are, and use the return for the result.

    node* findKthNode (node* head, int find, int& found){
        if(!head) {
            found = 1;
            return head;
        }
        node* retval = findkthNode(head->next, find, found);
        if(found==find)
            retval = head;
        found = found + 1;
        return retval;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this question has probably been asked already but I would really like
I know this question has already been asked but I am in urgent need
First of all, I know this question has already been asked but the answer
I know this question has already been asked, but the solution doesn't work in
I know that this question has already been asked HERE but sadly none of
I know this question has been asked before, but I ran into a problem.
I know this question has been asked a bit before. But looking around I
I know this specific question has been asked before , but I am not
I know this question has been asked before, but I tried eveything and can't
I know this question has been asked many times. But from what I read,

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.