• 0

Given a singly linked list find if it is palindrome

Problem description :

Write a function to determine if a singly linked list is a palindrome.

Input : A linked list
Output : Boolean (true or false)


Approach 1: Reverse and compare the linked lists.

Logic :

  • Create a new copy of the linked list.
  • Reverse the newly created linked list.
  • Compare the original linked list with the reversed linked list.

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 and Reverse a singly linked list. I recommend reading that first, as the following code uses the method from it.

Solution :