GeekInterview.com
Series: Subject: Topic:
Question: 16 of 56

Printing Linked List In a Reverse Order Using Head Pointer

How to write a program to print a linked list in reverse order by using only one head pointer[without moving the head pointer].
Asked by: naga.t | Member Since Jul-2012 | Asked on: Jul 6th, 2012

View all questions by naga.t

Showing Answers 1 - 1 of 1 Answers
Sereche

Answered On : Jul 8th, 2012

View all answers by Sereche

The fastest way to do this is to traverse the linked list forward once, saving the value at each node (or a pointer to the node) in a backwards-traversable data structure. If you know the size of the list beforehand, an array would be most efficient; otherwise, a dynamic array is probably best. You could then iterate through this structure in reverse order efficiently, printing the value at each node.

If memory usage is a concern (i.e., your system might not have enough memory to save the address to every node), your best bet is a divide-and-conquer approach. Since you most likely wont have that many items, I wont go into further detail here (and if you do, I would strongly urge you to consider a different data structure, as well as refraining from printing out every entry).

I dont understand the constraint regarding the head pointer; the list should not be modified in any way by a program that only prints its contents.

  
Login to rate this answer.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Question Categories

Analytical Ability Interview Questions

Aptitude Interview Questions

Career Goals Interview Questions

Creativity Interview Questions

Critical Reasoning Questions

Decisiveness Interview Questions

Flexibility Interview Questions

Group Discussions Topics

HR Interview Questions

Interests Interview Questions

Job Performance Interview Questions

Leadership Interview Questions

Motivation Interview Questions

Personal Interview Questions

Placement Assistance

Challenging Puzzles

Quantitative Aptitude Questions

Self Assessment Interview Questions

Skills Interview Questions

Verbal Ability Questions

Warm-Up Interview Questions

Work History Interview Questions

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.