Reverse a Linked List
Contents
How to reverse a linked list
- Initialize
- null
prev
curr
node pointing to head
- null
- Change directions
- save
curr
’s next into a variablenext
- point
curr
’s next pointer toprev
- save
- Move the
curr
andprev
nodes one step aheadprev
becomescurr
curr
becomesnext
- Repeat steps 2 and 3 until
curr
isNULL
Code for reversing a linked list
Python
|
|
Notes
- I will be modifying the infographic to be more minimalistic. Maybe will think of doing a flashcard of all the important and frequent concepts required for passing the interviews.
- The most important aspect of reversing a linked list is to remember to use a dummy null node at the beginning. It seems to be a re-occuring theme to initialize a dummy node in many linked list problems. I will try to find that pattern and write an article about it.