-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path234. Palindrome Linked List.py
65 lines (49 loc) · 1.29 KB
/
234. Palindrome Linked List.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
lreverse=[]
def traverseinreverse(head):
if not head: #base case, empty list
return
traverseinreverse(head.next)
lreverse.append(head.val)
return lreverse
list=[]
def traverselist(head):
temp=head
while temp:
list.append(temp.val)
temp=temp.next
return list
def isPalindrome():
if not head:
return True
traverseinreverse(head)
traverselist(head)
#i,j=0,len(list)-1
#while i!=len(list)-1 and j!=0:
#if list[i]==lreverse[j]:
#i+=1
#j-=1
#else: return "false"
#return "true"
return lreverse==list
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def isPalindrome(self, head: ListNode) -> bool:
if not head:
return True
if head.next is None:
return True
address = head.next
res = [head.val]
while address:
res.append(address.val)
address = address.next
return res == res[::-1]