class ListNode:
def __init__(self, item):
self.val = item
self.next = None
class LinkedList:
def rear_insert(self, li):
if not li:
return None
head = ListNode(li[0])
tail = head
for val in li[1:]:
node = ListNode(val)
tail.next = node
tail = node
return head
def print(self, head):
current = head
while current:
print(current.val, end="->")
current = current.next
print(None)
li = [1, 2, 1]
l = LinkedList()
res = l.rear_insert(li)
# 通过栈的pop函数解题
class Solution:
def isPalindrome(self, head: ListNode) -> bool:
stack = []
tmp = head
while tmp:
stack.append(tmp.val)
tmp = tmp.next
while head:
if head.val != stack.pop():
return False
head = head.next
return True
l.print(res)
ll = Solution()
print(ll.isPalindrome(res))
LCR 027. 回文链表
最新推荐文章于 2024-07-19 16:36:18 发布