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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:32:14+00:00 2026-05-31T07:32:14+00:00

I have an assignment in my java class that i need to recursively print

  • 0

I have an assignment in my java class that i need to recursively print out a linked list in reverse. I have looked online and found numerous examples of recursive methods that do this but take a node in as a parameter, and to my understanding i need to take in a linked list because i need to print the entire list out. Below is what code i have written, and it works in the sense that it prints out the list, recursively, but it is still in the same order that i created the list. After it prints out the list it throws a no such element exception as well. my main problem/question is wrapping my head around how best to print this recursively.

public void printRecurse2(LinkedList<String> list2)
{
    if(list2 == null)
        return;
    System.out.println(list2.pop());
    printRecurse2(list2);

}
  • 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-31T07:32:15+00:00Added an answer on May 31, 2026 at 7:32 am

    Your list will never be null, this is why the recursion doesn’t stop in time. You need to check whether the list is not empty, instead of for null, e.g.

    if(list2.peek() == 0)
        return;
    System.out.println(list2.pop());
    printRecurse2(list2);
    

    This avoids the exception. To print in reverse, you need to start from the other end of the list:

    if(list2.peekLast() == null)
        return;
    System.out.println(list2.pollLast());
    printRecurse2(list2);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a college assignment where I need to print out items sold by
I am working on a short java assignment that I have been set. The
I'm taking a beginning Java class and an assignment requires that I write a
I have a Assignment question about Java Generics. The given class is Product.java. It
I have an assignment for a class that is to be done in C#.
Working on my assignment for Java and I have created a class called Triangle.
So in my java class we have a homework assignment to use System.currentTimeMillis to
I'm in a data structures class and we have an assignment that includes creating
I have to write a multiplayer pacman game in Java for a university assignment
I have an assignment in a language-independent class, and part of it is using

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.