From bba5e053a2dc0228ed4dc6444cada92257ace256 Mon Sep 17 00:00:00 2001 From: Shahriar Shatil <52494840+ShatilKhan@users.noreply.github.com> Date: Fri, 22 Mar 2024 19:33:26 +0600 Subject: [PATCH] Time: 295 ms (49.62%) | Memory: 37 MB (26.49%) - LeetSync --- .../palindrome-linked-list.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 234-palindrome-linked-list/palindrome-linked-list.py diff --git a/234-palindrome-linked-list/palindrome-linked-list.py b/234-palindrome-linked-list/palindrome-linked-list.py new file mode 100644 index 0000000..459d9e4 --- /dev/null +++ b/234-palindrome-linked-list/palindrome-linked-list.py @@ -0,0 +1,18 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution: + def isPalindrome(self, head: Optional[ListNode]) -> bool: + list_vals = [] + while head: + list_vals.append(head.val) + head = head.next + + left, right = 0, len(list_vals) - 1 + while left < right and list_vals[left] == list_vals[right]: + left += 1 + right -= 1 + return left >= right + \ No newline at end of file