-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy path016-palindrome.py
More file actions
29 lines (24 loc) · 851 Bytes
/
016-palindrome.py
File metadata and controls
29 lines (24 loc) · 851 Bytes
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
from linked_list import LinkedList
class MyLinkedList(LinkedList):
def is_palindrome(self):
if self.head is None or self.head.next is None:
return False
curr = self.head
reversed_list = MyLinkedList()
length = 0
# Reverse the linked list
while curr is not None:
reversed_list.insert_to_front(curr.data)
length += 1
curr = curr.next
# Compare the reversed list with the original list
# Only need to compare the first half
iterations = length // 2
curr = self.head
curr_reversed = reversed_list.head
for _ in range(iterations):
if curr.data != curr_reversed.data:
return False
curr = curr.next
curr_reversed = curr_reversed.next
return True