Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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

Which data structures are used in bfs and dfs algorithm?

974


Which sorting technique is faster?

839


Do sets allow duplicates?

889


What do you mean by external sorting?

820


Define a relation?

884


What is difference between list and array list?

805


Explain the uses of binary tree.

861


What the principle of quick sort and its complexity?

923


Differentiate between hashmap and hashtable.

1047


write a program to show the insertion and deletion of an element in an array using the position

829


What are the Advantages and disadvantages of Array?

1056


Write a data structure for a queue.

1153


What do you mean by double hashing?

1000


Give the example of validating the parenthesis of expression using stack.

976


What does the dummy header in linked list contain?

1044