-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path104_Google_Is_Doubly_LinkedList_Palindrome.py
executable file
·54 lines (36 loc) · 1.34 KB
/
104_Google_Is_Doubly_LinkedList_Palindrome.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
"""Good morning! Here's your coding interview problem for today.
This problem was asked by Google.
Determine whether a doubly linked list is a palindrome. What if it’s singly linked?
For example, 1 -> 4 -> 3 -> 4 -> 1 returns True while 1 -> 4 returns False."""
# works for both singly and doubly linked lists
class LinkedList:
def __init__(self, data, nxt=None):
self.data = data
self.nxt = nxt
def __repr__(self):
return "{}->{}".format(self.data, self.nxt)
def is_ll_palindrome(head):
# store the Linked list into a string
string = ""
# traverse linked list
curr = head
while curr:
string = string + str(curr.data)
curr = curr.nxt
temp = string[::-1]
return string == string[::-1]
# basically this is storing the pointer to the elements of the linked list in a call stack
def is_ll_palindrome_redux(head):
left = head
def helper(right):
nonlocal left
if right.nxt is None:
return left.data == right.data
if helper(right.nxt) is False:
return False
left = left.nxt
return left.data == right.data
return helper(head)
if __name__ == '__main__':
ll1 = LinkedList('a', nxt=LinkedList('b', nxt=LinkedList('c', nxt=LinkedList('b', nxt=LinkedList('a')))))
print(is_ll_palindrome_redux(head=ll1))