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

The Archive Base Latest Questions

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

If I send my reverse function on a list I get the expected output.

  • 0

If I send my reverse function on a list I get the expected output. But if I use my reverseNth function I only get the first thing in my list. ReverseNth reverses the list in sections.
For example if I have a list = <1 2 3 4 5>. Calling reverse() will output <5 4 3 2 1>. Calling reverseNth(2) on the list should give <2 1 4 3 5>.

Relevant Code:

void List<T>::reverse( ListNode * & startPoint, ListNode * & endPoint )
{
    if(startPoint == NULL || startPoint == endPoint)
        return;
    ListNode* stop = endPoint;
    ListNode* temp = startPoint;
    startPoint = endPoint;
    endPoint = temp;
    ListNode* p = startPoint; //create a node and point to head

    while(p != stop)
    {
        temp = p->next;
        p->next = p->prev;
        p->prev = temp;
        p = p->next;
    }
}

ReverseNth code:

void List<T>::reverseNth( int n )
{
    if(head == NULL || head == tail || n == 1 || n == 0)
        return;

    if(n >= length)
    {
        reverse(head,tail);
        return;
    }

    ListNode* tempStart = head;
    ListNode* tempEnd;

    for(int j = 0; j < length; j += n)
    {
        // make the end of the section the beginning of the next
        tempEnd = tempStart;
        // set the end of the section to reverse
        for(int i = 0; i < n-1; i ++)
        {
            // check to make sure that the section doesn't go past the length
            if(j+i == length)
                i = n; 
            else
                tempEnd = tempEnd-> next;
        }

        reverse(tempStart, tempEnd);

        if( j == 0)
            head = tempStart;
        if(tempStart == tail)
        {
            tail = tempEnd;
            return;
        }
        else
            tempStart = tempEnd-> next;
    }
    tail = tempEnd;
}
  • 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-11T20:48:16+00:00Added an answer on June 11, 2026 at 8:48 pm

    You’re not using startPoint and endPoint in your reverse function. At present, your reverse function reverses the entire list, leaving the old head->next to point to null (since it’s now the end).

    I’m guessing the reverse function used to reverse the whole list, but was then extended to take an arbitrary start/end point (possibly an overload?).

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

Sidebar

Related Questions

I send 2 bytes of app data on the socket(blocking) every 10 seconds, but
I Send Iqueryable to a function that is below void Method<T>(ref IQueryable<T> qu) {
I have this reverse function which works in english, However it doesn't work with
I have a small C++ function which reverses a string in place: void reverse1(string&
To reverse lookup an url by means of name or View_name we will use
I don't understand this reverse dns thing at all. So, I have a website
I'm sending mails from my rails application but for some reason I never get
i have this function to get the content of file , #define BUFSIZE 512
I am trying to use Varnish as a reverse caching proxy for my nginx
to send a message in my application in android i have used the following

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.