-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathp2_6.py
60 lines (45 loc) · 1.17 KB
/
p2_6.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
from chapter_2.linked_list_utils import Node, llol
# Method: Slow/Fast pointer, push to stack second half and check
# Time: O(n)
# Space: O(n)
def check_ll_palindrome(head: Node):
slow_len = -1
fast_len = -1
if not head:
return True
slow_ptr = head
fast_ptr = head
stack = []
while fast_ptr:
slow_len += 1
fast_len += 2
stack.append(slow_ptr.val)
slow_ptr = slow_ptr.next
fast_ptr = fast_ptr.next
if fast_ptr:
fast_ptr = fast_ptr.next
if not fast_ptr:
fast_len += 1
if fast_len > 0 and fast_len % 2:
stack.pop()
print(f"Stack is {stack}, slp is {slow_ptr}")
while slow_ptr:
if slow_ptr.val != stack.pop():
return False
slow_ptr = slow_ptr.next
return not stack
if __name__ == "__main__":
exs = [
[],
[4],
[1, 2, 2],
[1, 2, 3, 4],
[1, 1, 2, 1],
[1, 1, 1],
[1, 1, 1, 1],
[1, 2, 0, 2, 1],
]
for ex in exs:
h_ex = llol(ex) if ex else None
print(f"Result for {ex} is {check_ll_palindrome(h_ex)}")
print("-"*50)