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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:13:37+00:00 2026-06-13T15:13:37+00:00

I am trying to reverse a doubly linked list using iterative approach and i’m

  • 0

I am trying to reverse a doubly linked list using iterative approach and i’m struck on the part where my original list is being modified even though i’m not using a pointer to pointer style modification of original list.

This is the reverse function i am using, head is a local variable declared in main
like Node* head = NULL; and is already populated with a set of values.

Node* reverse_iterative(Node* head)
{
    if(head == NULL || head->next == NULL)
        return head;

    Node *prev, *current=head, *temp;
    while(current != NULL)
    {
        current->prev = current->next;
        current->next = temp;
        temp = current;
        current = current->prev;
    }
    return temp;
}

This is how i used this in main :

Node* rev = NULL;
rev = reverse_iterative(head);

Here is the output i got:

original list: 15 <=>25 <=>35 <=>45 <=>55 <=>65 <=>75 <=>
making the list actually reverse: 75 <=>65 <=>55 <=>45 <=>35 <=>25 <=>15 <=>
after reversing, the original list now: 15 <=>

I couldn’t get the part where my original head node was being modified.

  • 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-13T15:13:38+00:00Added an answer on June 13, 2026 at 3:13 pm

    Look carefully at your while-loop, and consider the following statement on the initial iteration of the loop:

    current->next = temp;
    

    What is in temp at the time of this assignment? Exposing more of the code, we have:

    Node *prev, *current=head, *temp;
    while(current != NULL)
    {
        current->prev = current->next;
        current->next = temp;
        temp = current;
        current = current->prev;
    }
    

    Note that temp is uninitialized and thus an undefined pointer value. Your original head-ptr next ptr is being reset to garbage held in the uninitialized temp.

    Fix this by doing the following:

    Node *current=head, *temp=NULL;
    while(current != NULL)
    {
        temp = current->prev;
        current->prev = current->next;
        current->next = temp;
        temp = current;
        current = current->prev;
    }
    

    I’ve not had a chance to test it, but that should get you closer to what you’re looking for.

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

Sidebar

Related Questions

I'm trying to reverse a linked list by using C++ and then print out
I've been trying to figure out how to reverse the order of a doubly-linked
Im trying to reverse the order of the following linked list, I've done so,
Possible Duplicate: Unable to reverse a linked list I'm trying to reverse a linked
I was trying to reverse a linked list, however whenever I execute the following
I am trying to reverse dns a list of IPs using socket.gethostbyaddr() in python,
I am trying to reverse a linked list. This is the code I have
I'm currently trying to reverse geocode a series of lat/long co-ordinates using the Virtual
I am trying to reverse engineer pojos (using hibernate tools plugin v3.2.4x in eclipse
I am trying to reverse geocode my location using google maps to retrieve my

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.