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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:44:16+00:00 2026-05-27T14:44:16+00:00

I know questions about reversing linked lists have been asked before but I want

  • 0

I know questions about reversing linked lists have been asked before but I want to try to figure this out with my own implementation.

My code makes sense to me and I feel it should work but when I run the debugger, the loop runs infinitely. Specifically, head and next keep alternating between the first and second nodes of the linked list. The getLink() method returns the pointer of the node (which is pointing to the next node in the list). Any input based on my comments in under the while loop would help.

void revNodes(IntNodePtr& head)
{
    IntNodePtr prev = NULL;
    IntNodePtr next = NULL;
    next = head;
    while (next != NULL)
    {
        prev = head;
        // this should advance the pointer head to the next node in the list because next is the same as head initially
        head = next->getLink();
        // advance the pointer next to node after it
        next = next->getLink();
        // set the pointer in the node that head is pointing to to prev (head before head was advanced)
        head->setLink(prev);
    }
}
  • 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-27T14:44:17+00:00Added an answer on May 27, 2026 at 2:44 pm

    You initialise next to head and then inside the loop you’re setting them to both point to the same node.

    Effectively, they’re both pointing at the first node after the head of the list, you’re then setting the pointer on that node to be the head of the list with the line head->setLink(prev). Therefore, on your next pass through the loop, head and next both get pointed back at the original head node!

    If you change:

    next = next->getLink();
    

    to:

    next = head->getLink();
    

    You’ll be able to progress through the list, but then you’ll jump the next node as you set head to next->getLink(). So at the start you should initialise next to be head->getLink(), set head to next inside the loop, and next to head->getLink() as above.

    So turning that into code:

    void revNodes(IntNodePtr& head)
    {
        IntNodePtr prev = NULL;
        IntNodePtr next = NULL;
        next = head->getLink();
        while (next != NULL)
        {
            prev = head;
            // this should advance the pointer head to the next node in the list
            head = next;
            // advance the pointer next to node after it
            next = next->getLink();
            // set the pointer in the node that head is pointing to to prev (head before head was advanced)
            head->setLink(prev);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know similar questions have been asked, but I'm not sure about the answers
I know there's a lot of questions about this but I've been struggling with
I know this has been asked a million times before but i still can't
I know the question about measuring developer performance has been asked to death, but
I know there are several questions about satellite Assembly's out there, but I still
I know that there are many different questions about this sort of topic on
Okay I know there are a lot of questions about this and a lot
ok, I know there are a lot of questions here about this, that are
I know there are plenty of questions here already about this topic (I've read
i know there are several questions about regex recursion in .net. I can write

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.