-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path234.py
40 lines (36 loc) · 1.1 KB
/
234.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# Palindrome Linked List
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
# def reverseList(head: ListNode) -> ListNode:
# reverseList = ListNode(head.val)
# head = head.next
# while head != None:
# newNode = ListNode(head.val)
# newNode.next = reverseList
# reverseList = newNode
# head = head.next
# return reverseList
# class Solution:
# def isPalindrome(self, head: ListNode) -> bool:
# if head is None:
# return True
# reversedList = reverseList(head)
# while reversedList != None and head != None:
# if reversedList.val == head.val:
# reversedList = reversedList.next
# head = head.next
# else:
# return False
# return True
class Solution:
def isPalindrome(self, head: ListNode) -> bool:
if not head:
return True
s=[]
while head:
s.append(head.val)
head = head.next
return s==s[::-1]