create an singly linked lists and reverse the lists by
interchanging the links and not the data?

Answer Posted / salil

This is the same solution as many have given but I have
tried to make it little easier to understand. The reversal
process can be imagined as creating a new list of nodes
coming out of the old list. Assuming that the two lists are
known by their heads oldhead (pointing to the old list) and
new head (pointing to the new list). Basically the steps
involved are:
- move the current node from the old list to the new list
- the new node coming from the old list comes to the head
of the new list
- make the new node point to the head of the new list you
had so far - also means the new node becomes the new head
for the new list
- adjust the heads of the new and old list
- do these steps until the oldhead points to null. of
course, you will need to start with the newhead being null.

reverse(struct node *head)
{
struct node *oldhead,*newhead,*t;
oldhead=head;
newhead= NULL;
while(oldhead!=NULL)
{
t=newhead; (save current newhead)
newhead=oldhead; (newhead now points to current
node from old list)
oldhead=oldhead->next; (move oldhead forward)
newhead->next=t; (make new node point to head
of the new list so far)
}
head=newhead;

}

Is This Answer Correct ?    2 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the difference between b tree and binary search tree?

491


What is a data structure? What are the types of data structures? Briefly explain them

547


List out the basic operations that can be performed on a stack?

463


What is the difference between Array and Arraylist?

610


What is meant by binary tree?

468






There is a program which inserts and deletes node in a sorted singly linked list. There is a bug in one of the modules, how would you debug it?

1317


What are the disadvantages of sequential storage?

736


Which sorting technique is faster?

512


How to compare Two Arrays?

547


Is hashset thread safe?

487


What is data structure explain in detail?

517


Can we add or delete an element after assigning an array?

670


Is hashset synchronized?

516


What is a vector class?

491


Can you change size of array once created?

459