meta data for this page
Palindrome Linked List
Given the head
of a singly linked list, return true
if it is a palindrome orfalse
otherwise.
Example 1:
Input: head = [1,2,2,1] Output: true
jump to: Search / User Tools / Main Content / Change Content Width
Software Quality Engineering & Beyond
You are here: SQE & Beyond » Interview Questions » Algorithm Interview Questions » Palindrome Linked List
Given the head
of a singly linked list, return true
if it is a palindrome orfalse
otherwise.
Example 1:
Input: head = [1,2,2,1] Output: true
class Solution { private ListNode head; public boolean isPalindrome(ListNode head) { this.head = head; return compare(head); } private boolean compare(ListNode tail) { if (tail == null) return true; boolean matched = compare(tail.next) && head.val == tail.val; head = head.next; return matched; } }