v9.1.1
Menu

Palindrome Linked List

easyFREELinked Lists & PointersApple DSA

Given a linked list (as comma-separated values), return True if it is a palindrome.

Example

Input
1,2,2,1
Output
True
Reasoning
  • The input linked list is 1,2,2,1, which can be visualized as a sequence of nodes with values 1→2→2→11 \rightarrow 2 \rightarrow 2 \rightarrow 1.
  • To check if it's a palindrome, we compare the first and last nodes, then the second and second-to-last nodes, and so on.
  • Since the values are symmetric (1=11 = 1 and 2=22 = 2), the linked list is a palindrome.
  • The function returns True because the input linked list reads the same backward as forward.

Constraints

  • 1 <= number of nodes <= 10^5
  • 0 <= Node.val <= 9

Learn this in

More from Apple DSA

solution.py

Test results

Sign in to run your code against the test cases.