forked from adnanaziz/EPIJudge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
is_list_palindromic.py
27 lines (21 loc) · 947 Bytes
/
is_list_palindromic.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
from list_node import ListNode
from reverse_list import reverse_list
from test_framework import generic_test
def is_linked_list_a_palindrome(L: ListNode) -> bool:
# Finds the second half of L.
slow = fast = L
while fast and fast.next:
fast, slow = fast.next.next, slow.next
# Compares the first half and the reversed second half lists.
first_half_iter, second_half_iter = L, reverse_list(slow)
while second_half_iter and first_half_iter:
if second_half_iter.data != first_half_iter.data:
return False
second_half_iter, first_half_iter = (second_half_iter.next,
first_half_iter.next)
return True
if __name__ == '__main__':
exit(
generic_test.generic_test_main('is_list_palindromic.py',
'is_list_palindromic.tsv',
is_linked_list_a_palindrome))