Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Reddit
Contact us
Hide Buttons

Reverse a singly linked list

Problem description :

Write a function to reverse a singly linked list.

Input : A linked list
Output : A linked list

reverse-list-min

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.

Solution :

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *