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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:47:05+00:00 2026-06-06T12:47:05+00:00

This method right below reverses a doubly linked list with n elements. I dont

  • 0

This method right below reverses a doubly linked list with n elements. I dont understand how this really works. I have added comments, please correct me if I am wrong. I am not sure how the traversing process works.

 public void reverseDLL( ) {
   Node temp=head; //swap head and tail
   head=tail; // head now points to tail
   tail=temp; //tail points to head
    //traverse the list swapping prev and next fields of each node
  Node p=head; //create a node and point to head

  while(p!=null) //while p does not equal null
    { //swap prev and next of current node
      temp=p.next; // p.next does that not equal null? confusing.
      p.next=p.prev; //this line makes sense since you have to reverse the link
      p.prev=temp; //having trouble visualizing this.
      p=p.next;//advance current node which makes sense
    }
 }
  • 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-06T12:47:08+00:00Added an answer on June 6, 2026 at 12:47 pm

    Let’s try stepping through the code a few lines at a time.

    Node temp=head;
    head=tail;
    tail=temp;
    

    Here we are just setting up some variables. We are swapping our head to point to the tail and the tail to the head.

    Now we define our starting node. This is our new head that used to be the tail.

    Node p=head; //create a node and point to head
    
    while(p!=null)
    { 
        temp=p.next; 
    

    At this point, this is what we are looking at (note: if this is the first iteration, next would point to null but that doesn’t matter, just assume A is null for that case):
    enter image description here

    So we have next pointing to A and prev pointing to B. We want these to be swapped. To do so, we go ahead and assign next to prev (which points to B) so now next and prev both point to B.

        p.next=p.prev; 
    

    Great! We’re half way there. Now we have:

    Step 2

    Now our last step is to have prev point to what next used to point to. How are we going to get to it? Luckily, we stored what next used to point to (in other words, A) in temp. So let’s use that to assign prev.

        p.prev=temp; 
    

    Alas, we have:

    enter image description here

    Now this node has been swapped, and we move on to the next.

        p=p.next;
    }
    

    Rinse and repeat.

    All together:

    Node p=head; //create a node and point to head
    
    while(p!=null)
    { 
        temp=p.next; 
        p.next=p.prev; 
        p.prev=temp; 
        p=p.next;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to get this right but i dont know what i
This method is working totally right in matlab. but, when I compiled it in
I never seem to get this right. I've got a method that returns a
This method works as expected - it creates a JTree with a root node
Take this method /** * @return List of group IDs the person belongs to
I have method to which I pass an object. In this method I check
I have this setting below, but when I touch the table cell, I get
I have a method as shown below... public bool MakeRequest(string[] args) { try {
How do I implement this method (see below)? I'm new to Objective-C and I'm
This method that draws my tiles seems to be quite slow, Im not sure

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.