Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com

Check If Linked List Is Palindrome

Easy
0/40
Average time to solve is 15m
profile
Contributed by
81 upvotes
Asked in companies
Thought WorksTata Consultancy Services (TCS)Quikr

Problem statement

You are given a Singly Linked List of integers. You have to return true if the linked list is palindrome, else return false.


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


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.
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first and only line contains the linked list of elements separated by a single space and terminated by -1. 
Hence -1 would never be a list element.
Output Format :
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.
Sample Input 1:
1 2 2 1 -1
Sample Output 1:
True
Explanation for Sample Input 1:
The given list is a palindrome.
Sample Input 2:
3 2 1 -1
Sample Output 2:
False
Constraints :
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 list's nodes.

Time Limit: 1sec
Full screen
Console