Palindrome Linked List
Palindrome Linked List
Given a singly linked list, determine whether the sequence of node values reads the same forward and backward (i.e., it is a palindrome). For example, the list 1→2→3→2→1 is a palindrome, while 1→2→3 is not. Example: n=5, values=[1 2 3 2 1] → YES Example: n=3, values=[1 2 3] → NO
Input format — only if your code reads from stdin
Input format: - First line: integer n (number of nodes) - Second line: n space-separated integers (node values from head to tail) Output: YES if the list is a palindrome, NO otherwise.
Approach
O(n) time, O(1) extra space approach: use slow/fast pointers to find the midpoint, reverse the second half in-place, then compare the two halves node by node. Restore the list if needed. Simpler O(n) space: push all values to an array and use two pointers from both ends.
Input
Output
- •
1 ≤ n ≤ 10^5, -10^9 ≤ node values ≤ 10^9
More Linked Lists problems
Browse all coding problems →Press Run to test your custom input, or Submit to judge against all test cases.
Sign in to continue
Create a free account to run code, submit solutions and track progress.
- Free forever — no credit card
- 10+ languages with live execution
- Points, streaks & leaderboards