234. Palindrome Linked List
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def cloneLinkedList(self, head) -> ListNode:
copy = copy_head = ListNode(0)
curr = head
while curr:
copy.next = ListNode(curr.val)
copy = copy.next
curr = curr.next
return copy_head.next
def reverseList(self, head: ListNode) -> ListNode:
node = None
while head:
tmp = head.next
head.next = node
node = head
head = tmp
return node
def isPalindrome(self, head: ListNode) -> bool:
cloned = self.reverseList(self.cloneLinkedList(head))
curr = head
while curr:
if curr.val != cloned.val:
return False
curr = curr.next
cloned = cloned.next
return TrueLast updated