Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Class notes

Reverse a Linked List | Iteratively & Recursively |

Rating
-
Sold
-
Pages
2
Uploaded on
28-03-2023
Written in
2022/2023

Reverse a Linked List | Iteratively & Recursively |

Institution
Course

Content preview

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

Written for

Institution
Course

Document information

Uploaded on
March 28, 2023
Number of pages
2
Written in
2022/2023
Type
Class notes
Professor(s)
Ashish singh
Contains
All classes

Subjects

$3.49
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
ashishsingh92003

Get to know the seller

Seller avatar
ashishsingh92003 Alabama State University
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
3 year
Number of followers
0
Documents
1
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions