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

Answers were Sorted based on User's Feedback



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

Answer / vijayan

struct node{
int data;
node * next;
};
node *pt1,*pt2=NULL:
while(root!=NULL)
{
pt1=root;
root=root->next;
pt1->next=pt2;
pt2=pt1;
}

Is This Answer Correct ?    31 Yes 3 No

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

Answer / bharath

I am correcting Vaishali's method here,

We can achive this using following method:
Use three pointers
First is start pointing to first node.
Second is prev pointing to second node
Third is curr pointing to third node.

start->next=NULL;
while(start!=curr)
{
prev->next=start
start=prev;
prev=curr;
curr=curr->next;
}
This reverses the list.

Is This Answer Correct ?    3 Yes 0 No

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

Answer / kushal bagaria

struct node
{
int data;
struct node *list;
};

reverse(&p) /* p is the pointer to the 1st node of ll*/

function reverse:-

reverse(struct node **q)
{
struct node *r,*t,*prev;
r=*q;
prev= NULL;
while(r!=NULL)
{
t=prev;
prev=r;
r=r->link;
prev->link=t;
}
*q=prev; /* last node bcoms d root of ll */
}

Is This Answer Correct ?    3 Yes 0 No

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

Answer / bhanu yadav

reverse(node *first) //first address of first node in linked
{ node *x,*temp,*ttemp;
temp=first; //temp at first
ttemp=temp->next; //ttemp next to temp

while(temp->next!=null)
{ x=ttemp->next;
ttemp->next=temp;
temp=ttemp;
ttemp=x;
}
}

Is This Answer Correct ?    3 Yes 1 No

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

Answer / 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

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

Answer / mayur bagal

struct node{
int data;
node * next;
};

node *pt2=NULL:
while(root!=NULL)
{
node * temp = root->next;
root->next= pt2;
pt2=root;
root = temp;
}

Is This Answer Correct ?    3 Yes 2 No

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

Answer / vimesh

struct node
{
int data;
node *link;
};
node *p=NULL;
void reverse(*x)
{
node *r,*s,*q;
r=NULL;
q=x
while(q->link!=NULL)
{
s=r;
r=q;
q=q->link;
r->link=s;
}
x=r;
}

Is This Answer Correct ?    2 Yes 1 No

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

Answer / nash

If the Linked list is small enough i'd use a recursive function.

reverse(head, head, NULL);

void reverse(Node* headNode, Node* currNode, Node* prevNode)
{
if(headNode != NULL && currNode != NULL)
{
reverse(currNode.next, currNode);
}
else
{
headNode = currNode; // Reached the end of the list.
}

currNode.next = prevNode;
}

Is This Answer Correct ?    1 Yes 0 No

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

Answer / jishu

This is a more efficient version of the program given in ans 2.

p=head;
result=NULL;

while(p!=NULL)
{
temp=p->next;

p->next=result;
result=p;

p=temp;

}

Is This Answer Correct ?    2 Yes 1 No

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

Answer / sumit garg( jmit mca)

\\HELLO FRIENDS CHEK THIS...IT IS TESTED\\
struct node
{
int info;
struct node * next;
};
struct node * ptr=NULL,* old=NULL,*save=NULL;
void reverse()
{
old=ptr=start;
while (ptr!=NULL)
{
ptr=ptr->next;
old->next=save;
push(old);
save=old;
old=ptr;
}
}
void push(struct node * p)
{
top++;
stack [top]=p;
}
struct node * pop()
{
struct node * ret=NULL;
ret=stack[top];
top=top-1;
return ret;
}
display(struct node * disp)
{
disp=stack[top];
while (disp->next!=NULL)
{
disp=pop();
printf("%d",disp->info);
}

Is This Answer Correct ?    2 Yes 3 No

Post New Answer

More Data Structures Interview Questions

Can arraylist have null values?

0 Answers  


To describe the Complexity of Binary search, Quicksort and various other sorting and searching techniques..

0 Answers   HPCL, Hughes Systique Corporation,


What does stack top do?

0 Answers  


Explain binary representation?

0 Answers  


Why merge sort is better than insertion sort?

0 Answers  






What are the Difference between tcp and udp?

0 Answers  


What is the difference between list and arraylist?

0 Answers  


Define primary data structures?

0 Answers  


How do you find the size of an arraylist?

0 Answers  


Differentiate between iterator and listiterator.

0 Answers  


Which is best book for data structures?

0 Answers  


Is quicksort faster than merge sort?

0 Answers  


Categories