Check If Linked List Is Palindrome

You are given a Singly Linked List of integers. You have to find if the given linked list is palindrome or not.

A List is a palindrome if it reads the same from the left to the right and from the right to the left.

For example, the lists (1 -> 2 -> 1), (3 -> 4 -> 4-> 3), and (1) are palindromes, while the lists (1 -> 2 -> 3) and (3 -> 4) are not.

Input Format:
The first line of input contains a single integer T, representing the number of test cases or queries to be run. 

Then the T test cases follow.

The first and only line of each test case contains the elements of the linked list separated by a single space and terminated by -1. Hence, -1 would never be a list element.
Output Format :
For each test case, print “True” if the given linked list is a palindrome, else print “False”.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 10
1 <= N <= 5 * 10^4
-10^9 <= data <= 10^9 and data != -1

Where N is the number of nodes in the linked list and ‘data’ represents the value of the nodes of the list.

Time Limit: 1sec
CodingNinjas
author
2y

A simple approach could be to use a stack. Traverse the given list and push all the nodes to a stack. Now, Traverse the list again. For every visited node, pop the topmost node from the stack and comp...read more

CodingNinjas
author
2y
Using Stack

The idea is to store the list values in a stack and then compare the values in the list with the values in the stack.

Algorithm:

  1. Traverse the list from head to tail and push every node in th...read more
CodingNinjas
author
2y
Recursive Approach

Approach:

The idea is to reach the end of the list and then compare if the last node has the same value as the first node, the second last node has the same value as the second node a...read more

CodingNinjas
author
2y
Dividing into two sub-lists

The idea is to divide the list into two halves from mid and reverse the second half. If both halves are equal then the given list is palindrome.

  • Divide the list into two list...read more
Add answer anonymously...
Quikr Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter