ntree.ai
ntree.aiPalindrome Linked List

Palindrome Linked List

mediumsolvedattempted

Given the head of a singly linked list, return true if the values read the same forwards and backwards.

Example 1:

1 → 2 → 2 → 1
Input:  head = [1, 2, 2, 1]
Output: true

Example 2:

1 → 2
Input:  head = [1, 2]
Output: false

Example 3:

Input:  head = []
Output: true
Explanation: an empty list reads the same either way.

Constraints:

  • 0 <= the number of nodes in the list <= 10^5
  • 0 <= Node.val <= 9
# Definitions:
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
 
def isPalindrome(head: ListNode) -> bool:
pass
 
1→2→2→1

Press Visualise to step through your code.