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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:54:24+00:00 2026-06-13T05:54:24+00:00

Possible Duplicate: Unable to reverse a linked list I’m trying to reverse a linked

  • 0

Possible Duplicate:
Unable to reverse a linked list

I’m trying to reverse a linked list:

void LinkedList::reverseList()
{
    Node *next=_head;
    Node *prev=0;
    while(next!=0)
    {
        Node *tmp=next->_next;
        next->_next=prev;
        prev=next;
        next=tmp;
    }
}

Lets say the list is: 4->3->2->1

When I print the list, I only see 1 (The print function is good).

Any help?

Thanks

  • 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-13T05:54:25+00:00Added an answer on June 13, 2026 at 5:54 am

    Since you said you wanted to find the problem on your own, I’ll just give you a hint instead of the solution.

    Your reverse function works in that it successfully reverses the list. That isn’t the problem. You probably have 2 calls to print. One before and one after the reverse. What do you note about the nodes being passed to print in both cases? What does that tell you?

    EDIT:

    Since you said you’ve found the problem, I’ll post the actual solution.

    In your reverse code, you never update the _head of the list, but when you reverse the list, the head does actually change from 4 to 1. Since you never update _head, when you call print the second time (after the reverse call) you start printing at 1, That being the end of the list, that’s the only node printed.

    The solution is to update _head when you reverse the list. The simplest way to do this is to simply update it in each iteration. This may be slightly less efficient than other possible solutions, but it doesn’t change the time complexity of the algorithm — it’s still O(n):

    void LinkedList::reverseList()
    {
        Node *next=_head;
        Node *prev=0;
        while(next!=0)
        {
            Node *tmp=next->_next;
            next->_next=prev;
            _head = next;
            prev=next;
            next=tmp;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Unable to get a list of installed Python modules How do I
Possible Duplicate: Unable to do File Upload in PHP I am trying to learn
Possible Duplicate: Unable to locate tools.jar I'm trying to use ant to compile using
Possible Duplicate: Unable to connect to internet in Blackberry device? I'm trying to send
Possible Duplicate: Android RuntimeException: Unable to instantiate the service I am trying to run
Possible Duplicate: Ivy fails to resolve a dependancy, unable to find cause I'm trying
Possible Duplicate: error: Unable to find vcvarsall.bat I'm trying to install MySql interface for
Possible Duplicate: Unable to get textfield value using jQuery I am trying to change
Possible Duplicate: Android RuntimeException: Unable to instantiate the service Trying to get GCM up
Possible Duplicate: Unable to Populate TableView In PopOverController - Objective C I am 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.