Reverse a Linked List | Iteratively &
Recursively |
IN this course will be talking about an important question in
linked list which is reverse a linked list.
one node can have only one next pointer 77 [UNK] > 00
[UNK] that we have assigned to 1. There is no next pointer
here.. we have lost the reference to this node, so what will
we do?
we will take a reference of this node. We will store it to a
temporary variable. we store it in temp. We will always have
a reference to temp. current node equals to head. previous
node before this will be my previous node. previous node ' a
k1 ' node. next of current equals to previous. This is current.
next for current will point the previous node. current will
point this & next will point previous. current is not equals to
null until our current is not null..
The code is a simple code that handles all our cases. Any
case you can think of suppose. It was a linked list of one
node, so it handles that as well. If the linked list is null
means head is already null. If head would have been null,
then current would also be null.. The code handles all cases
and you can check even if you have two or fifty nodes. we
will reverse the problem at the front and then we will break
this into a sub problem. We also did this previously in
iteration we solved the first part, so we broke the problem
into sub problem. Then we have to solve our small sub
problem. Similarly,. the work of this function is to return the
new head. This will return a new head. it will also reverse it
on your own and it will change these. link to reverse ones it
will do this on it's own. head is null or the next of head is
null. Then we have to handle that specially. I will tell you
later on first. Let 's understand the main logic. Main logic is
that first we called the function for reverse for head of next.
So our head was this so for it 's next we called a recursive
function. the job of this function is to reverse it on it's own
and return new head to me it did it's job & returned me new
head..
next of head is actually null. So as the next of this is null. We
will return the head so this function will return from here and
it will return 4. So can you see that our last element which is
going to be our new head is being returned. in this function
which was reverse 7. The new head in this function is the
Recursively |
IN this course will be talking about an important question in
linked list which is reverse a linked list.
one node can have only one next pointer 77 [UNK] > 00
[UNK] that we have assigned to 1. There is no next pointer
here.. we have lost the reference to this node, so what will
we do?
we will take a reference of this node. We will store it to a
temporary variable. we store it in temp. We will always have
a reference to temp. current node equals to head. previous
node before this will be my previous node. previous node ' a
k1 ' node. next of current equals to previous. This is current.
next for current will point the previous node. current will
point this & next will point previous. current is not equals to
null until our current is not null..
The code is a simple code that handles all our cases. Any
case you can think of suppose. It was a linked list of one
node, so it handles that as well. If the linked list is null
means head is already null. If head would have been null,
then current would also be null.. The code handles all cases
and you can check even if you have two or fifty nodes. we
will reverse the problem at the front and then we will break
this into a sub problem. We also did this previously in
iteration we solved the first part, so we broke the problem
into sub problem. Then we have to solve our small sub
problem. Similarly,. the work of this function is to return the
new head. This will return a new head. it will also reverse it
on your own and it will change these. link to reverse ones it
will do this on it's own. head is null or the next of head is
null. Then we have to handle that specially. I will tell you
later on first. Let 's understand the main logic. Main logic is
that first we called the function for reverse for head of next.
So our head was this so for it 's next we called a recursive
function. the job of this function is to reverse it on it's own
and return new head to me it did it's job & returned me new
head..
next of head is actually null. So as the next of this is null. We
will return the head so this function will return from here and
it will return 4. So can you see that our last element which is
going to be our new head is being returned. in this function
which was reverse 7. The new head in this function is the