VOOZH about

URL: https://www.geeksforgeeks.org/dsa/function-to-check-if-a-singly-linked-list-is-palindrome/

⇱ Palindrome Linked List - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Palindrome Linked List

Last Updated : 26 Aug, 2025

Given a singly linked list, we have to check whether it is a palindrome or not.
Examples:

Input:

👁 4

Output: true
Explanation: It forms the same sequence when read from front to back and from back to front.
Input:

👁 2

Output: false
Explanation: Not and Palindrome

[Approach - 1] Using Stack - O(n) Time and O(n) Space

The idea is to use stack and start traversing from the head node. Push all the node and then start comparing from the head node with top value of stack.

Steps to solve the problem:

  • Push all the node's data into the stack.
  • Again traverse from the head node.
  • Compare the popped node's data with the current node's data.
  • If both are not equal ,return false .
  • If all elements match ,return true.


Below is the implementation of the above approach : 


Output
true

[Approach - 2] Using Recursion - O(n) Time and O(n) Space

The idea is to initialize a pointer start, which will initially point to the head of the node. Then, recursively traverse the list. At each node end, first recursively check if the right side of the list is palindrome. If yes, then compare the values of the start and end node. If they are equal, then set start = start.nextand return true. Otherwise return false.


Output
true

[Expected Approach] Using Iterative Method - O(n) Time and O(1) Space

The approach involves reversing the second half of the linked list starting from the middle. After reversing, traverse from the headof the list and the head of the reversed second half simultaneously, comparing the node values. If all corresponding nodes have equal values, the list is a palindrome.

Follow the steps below to solve the problem:

  • Use two pointers , fast and slow to find the middle of the list. fast will move twosteps ahead and slowwill move one step ahead at a time.
  • if list is odd, when fast->next is NULL , slow will point to the middle node.
  • if list is even, when fast->next->next is NULLslow will point to the middlenode.
  • Reverse the second half of the list starting from the middle.
  • Check if the first half and the reversed second half are identical by comparingthe node values.


Below is the implementation of the above approach:


Output
true
Comment