Checking if a linked list is a palindrome

A number is a palindrome if it reads the same forwards as it does backward. For example, the number 12333211233321 is a palindrome.

Linked lists can also be palindromes if they have the same order of elements when traversed forwards and backward​.

Algorithm

  • First, traverse the whole linked list, storing the value of each element in a stack at each step.
  • Traverse the whole linked list again, this time popping out elements from the stack and comparing them with the elements in the linked list. If all the elements are the same, then the linked list is a palindrome.
Traverse the linked list, pushing elements in a stack at each step.
1 of 9

Implementation

The following code implements the above algorithm.

Copyright ©2024 Educative, Inc. All rights reserved