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 are the disadvantages array implementations of linked list?

478


What is dynamic array with example?

549


What is list and its types?

505


What is a multidimensional array?

534


Differentiate between hashmap and treemap.

510






What are the issues that hamper the efficiency in sorting a file?

592


What are the types of collision resolution strategies in open addressing?

582


What is breadth first tree?

587


Differentiate between arraylist and linkedlist.

623


How many links are there in a binary tree of N nodes?

599


Which sorting has less time complexity?

508


What is raid (redundant array of inexpensive disks)?

541


What are the tasks performed during inorder traversal?

556


What are common data structures?

592


Differentiate between queue and deque.

511