Reverse a singly linked list
Problem description :
Write a function to reverse a singly linked list.
Input : A linked list
Output : A linked list
Logic :
- Iterate through linked list with three-pointers
previous
,current
,next
. - While iterating at each step reverse the current link's direction.
Time Complexity :
- O(n) ; where n is the number of nodes in the linked list
This post is a follow-up of JavaScript Linked List Example. I recommend reading that first, as the following code uses the method from it.
This post is a pre-requisite for Given a singly linked list find if it is palindrome. I recommend reading this first, before going to the palindrome post.