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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:11:12+00:00 2026-06-15T01:11:12+00:00

So, I have as part of my lesson in Computer Programming to reverse a

  • 0

So, I have as part of my lesson in Computer Programming to reverse a singly linked list of nodes, based on this algorithm

“Traversing the list sequentially, remove each node and insert it as the new first node.”

I was able to do this iteratively, but now my professor wants us to do this recursively.
I am trying my best to understand recursion, but its not working very well.

So, I changed my coding from iteratively to what I believe to be recursively

private void recursiveReverse2(Node p)
{
    Node lead = p;
    Node tail = p;

    if (p == null)
    {
        return;
    }

    if (p.next == null)
    {
        return;
    }

    current = tail.next;
    lead = current.next;
    current.next = null;
    tail.next = lead;
    current.next = head;
    head = current;
    recursiveReverse2(tail);
}


public void reverse2()
{
    toggle();   //swithces sort of list from ascending-descending
    recursiveReverse2(head); //head initialized at start of class
}

Basically, I wanted to ask if what I have done actually is recursion. Because, the recursiveReverse2() does work, but I just don’t know if I have implemented recursion or not.

  • 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-15T01:11:14+00:00Added an answer on June 15, 2026 at 1:11 am

    When writing recursion, it’s usually best to think about the end case(s), then write the recursive case last. The other thing about recursion is its extremely useful to return the result.

    Yes, your solution is technically recursive, but I don’t think the code works. At the line current.next = head, head is not defined, unless this code is in some class that you’ve not shown. Worse yet, it may infinite loop, because at the beginning of the function, tail = p and at the end your recursion is called with tail, thus an infinite loop. At best this will reverse a list of length 3, but not a list of any length.

    In Java, the recursive function often needs a “helper” function to get it started. First, assume the following node class:

    public class Node{
        public object data;
        public Node next;
    }
    

    And given the problem statement, I’m assuming we’re not allowed to play with the data pointers, just the next pointers. This code would be some other class than Node.

    public Node recursiveReverse(Node p)
    {
        return helperReverse(p, null);
    } 
    
    private Node helperReverse(Node p, Node previous)
    {
        if (p == null)
        {
            return p;
        }
        else if (p.next == null)
        {
            p.next == previous;
            return p;
        }
        else
        {
            Node next = p.next;
            p.next = previous;
            return helperReverse(next, p);
        }
    }
    

    It gets even better if it’s incorporated in the Node class.

    public class Node{
        public object data;
        public Node next;
    
        public Node reverse() {
            return reverse1(null);
        } 
    
        private Node reverse1(Node previous) {
            if (next == null) {
                next == previous;
                return this;
            }
            else {
                Node other = next;
                next = previous;
                return reverse1(other, this);
            } 
        }
    
    }
    

    Enjoy!

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

Sidebar

Related Questions

I have part of my script that goes like this: while read line do
I have this part of script from my GAE application which uses webapp2, which
I have some part of asynchronous code in my handlers, I need make this
I have this part of code that was compiling using ARMASM : /* Software
I have this part of a query: (@tmpo1245 := CAST( ( ( pm5.`meta_value` -
I have part of SQL Query. This SQL Query is no different than other,
I have part of the code below: while($array = $result->fetch_assoc() ){ $second_query = INSERT
i have part of Asp.NET 1.1 project. I work with remote site, which works
I have part objects and material objects. Each part has at most 1 material
I'm using an update panel to have part of my page update asynchronously. Its

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.