2.8 Reverse a Linked List - Iterative Method | Data
Structure Tutorials
Jenny's Lectures CS IT
In this video we'll use what iterative approach to reverse a linked list using loops and recursion.
In the first video we discuss the method we will apply the method here and after that we will
write down the code see how you can reverse this. After reversing of this linked list the output
should be something like this. Each node is going to contain address of its previous node so we
have to maintain the address of previous node also fine in this case right now previous node is
having zero right so now this link has been broken now fine so now we can not move here
because this link is broken. C in this case also we are going to create three functions one is
create linked list second one is reverse and third one is display the linked list. We have already
discussed the coding of those functions in the previous videos you can check out that we do in
this I button video.
Both current node and next node will contain whatever the value in head in head we have 100
so here hundred and here also hundred so now this is also pointing here and this is pointing to
the first node now we are going to start the traversing till where till here what condition you will
write obviously this means that this next would be zero. Next node is going to store address of
previous node so obviously we have to update the previous node value so now this previous
node should contain hundred. Next node using this next role pointed you can access this node
so next node next is 150 right so here you can write what current node is equal to next node.
Alexis says he has reversed all the links but one thing you have to take care header still pointing
here. Head should contain address of this node that is so from where you can get 300 no
problem we have previous node link and this is containing 300 that is why we are maintaining
this previous node so now after this while loop you can write what head is equal to previous
node.
The code will work even if you have only 1 node if only one node is there that is 5 is there then
output is also 5. value is 300 so now head is pointing here fine so as you can call the display
function fine and the output would be 7 1 6 and 5.
Structure Tutorials
Jenny's Lectures CS IT
In this video we'll use what iterative approach to reverse a linked list using loops and recursion.
In the first video we discuss the method we will apply the method here and after that we will
write down the code see how you can reverse this. After reversing of this linked list the output
should be something like this. Each node is going to contain address of its previous node so we
have to maintain the address of previous node also fine in this case right now previous node is
having zero right so now this link has been broken now fine so now we can not move here
because this link is broken. C in this case also we are going to create three functions one is
create linked list second one is reverse and third one is display the linked list. We have already
discussed the coding of those functions in the previous videos you can check out that we do in
this I button video.
Both current node and next node will contain whatever the value in head in head we have 100
so here hundred and here also hundred so now this is also pointing here and this is pointing to
the first node now we are going to start the traversing till where till here what condition you will
write obviously this means that this next would be zero. Next node is going to store address of
previous node so obviously we have to update the previous node value so now this previous
node should contain hundred. Next node using this next role pointed you can access this node
so next node next is 150 right so here you can write what current node is equal to next node.
Alexis says he has reversed all the links but one thing you have to take care header still pointing
here. Head should contain address of this node that is so from where you can get 300 no
problem we have previous node link and this is containing 300 that is why we are maintaining
this previous node so now after this while loop you can write what head is equal to previous
node.
The code will work even if you have only 1 node if only one node is there that is 5 is there then
output is also 5. value is 300 so now head is pointing here fine so as you can call the display
function fine and the output would be 7 1 6 and 5.