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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:18:15+00:00 2026-06-02T07:18:15+00:00

I have been working on a recursively defined list class in Python and I’m

  • 0

I have been working on a recursively defined list class in Python and I’m having trouble coding a reverse() method to function recursively. Here’s the basis for the class.

class RecList:
  def __init__(self):
    self._head = None
    self._rest = None

The base case would be self._head, being the first entry in a list, followed by the recursive case which is essentially another list containing its own self._head to start, then defined recursively. This proceeds all the way down to the bottom level where both self._head and self._rest = None. Is there a simple way to code a reverse method for a list defined like this?

  • 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-02T07:18:19+00:00Added an answer on June 2, 2026 at 7:18 am

    Try this:

    class RecList:
      def __init__(self, head=None, rest=None):
        self._head = head
        self._rest = rest
      def __str__(self):
        if self._rest is None:
            return str(self._head)
        return str(self._head) + ' ' + self._rest.__str__()
      def reverse(self):
        return self._reverse_aux(None)
      def _reverse_aux(self, acc):
        if self._rest is None:
            return RecList(self._head, acc)
        return self._rest._reverse_aux(RecList(self._head, acc))
    
    lst = RecList(1, RecList(2, RecList(3, None)))
    print lst
    > 1 2 3
    print lst.reverse()
    > 3 2 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have been having serious trouble getting an application we devlop running with UAC
I have been working on a Java project for a class for a while
I have been working on a method to sync core data stored in an
I have been working in Python for a few months now, and it has
I am doing this assignment and I am having trouble writing this method recursively.
I have been working on a recursive directory scan which scans folders and adds
Have been working on this question for a couple hours and have come close
I have been working on Eclipse recently. I am fairly new to java programming,
I have been working on a Windows Mobile app for a little while now
I have been working on keeping things object oriented for my project. Currently, I'm

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.