The Gateway to Computer Science Excellence
+1 vote
145 views

How D) could be answer here?

I think for head->next=temp; will not work properly. Am I doing any mistake?

in DS by Veteran (117k points) | 145 views
0
i think D must be the answer...

here we will store the next node ddress and point to first present node to the previous....
0

Say a linked list

1->2->3

Now according to code if we put option 4)

next=2

Now,

head->next=temp;

temp=head;

these two line how working?

How reverse operation going? Can u tell me in short?

0

srestha

head=1

temp=NULL;

next=head->next;(2)

head->next=temp;(pointing to previous node)

temp=head;

head=next;(2)

now again...

next=head->next;(3)

head->next=temp;(pointing to previous node 1)

temp=head;

head=next;(3)...

i suggest u to ....draw the diagram..and see t...

0
@hs

Is link also changing accordingly?

Means final one will be 3->2->1 ?

Not getting properly

plz help
0

@sresta...

here...

Procedure is like this....

. in original list head is pointig to 1 and  1 is pointing 2 and 2 is pointing to 3 and 3 is pointing to NULL...

In reversal...

1 will point to NULL...2 will point ot 1 and 3 will point to 2 and head will point to 3....

 Note;-- draw the procedure it on paper then u will get it definite...

1 Answer

0 votes

while(head)            // means while head is not equal to NULL

{

               next=head;

               head->next=temp;

               temp=head;

              head=next;

}

let us consider head is pointing to the current node

1. is the first node

now we have to do only change head->next=NULL

2.not the first node

now we have to assign head->next= // previous node so we have to keep track of the previous node so that assignment can be possible

that why we use the  *temp to store previous node that can be assigned to head->next   such that head->next=tmp 

* for the first node temp is simply null

now initially if we assign head->next=temp then we cannot keep track of the current next node because my current next node link will be lost that's why it used *next to first store the link of the current next node and the update head->next=temp

after that the current node is stored in the temp and head is moved to the current next node 

by (221 points)

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,654 questions
56,169 answers
193,877 comments
94,299 users