1 Star 0 Fork 0

蓝桥云课/python-100

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
016-palindrome.py 851 Bytes
一键复制 编辑 原始数据 按行查看 历史
xiaoyi733112 提交于 2020-02-19 17:50 . python-100 answer
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
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/lanqiao-courses/python-100.git
git@gitee.com:lanqiao-courses/python-100.git
lanqiao-courses
python-100
python-100
master

搜索帮助