-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathisListPalindrome.py
43 lines (31 loc) · 991 Bytes
/
isListPalindrome.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
#Singly-linked lists are already defined with this interface:
class ListNode(object):
def __init__(self, x):
self.value = x
self.next = None
'''
Algorithm: O(n) time, 0(n) space
1. For every element in the LL, fill it into a Array
2. After the array is filled, reverse one the array
3. Compare the reversed array to original array for equality
Note: Cost about n space and we have to reverse one array
'''
def isListPalindrome(head):
if not head:
return True
arr = []
while head:
arr.append(head.value)
head = head.next
return arr == arr[::-1]
head_True = [0, 1, 0]
head_False = [1, 2, 2, 3]
# isListPalindrome(head_True )
def dicReturntest(sample, n):
if sample == None:
return 0
for idx,value in sample.items():
if idx == n or value ==n:
return sample[idx]
test = {'E': '5', 'D': '7', 'M': '1'}
print("Returned value: " + str(dicReturntest(test,"5")))