-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path234.回文链表.py
36 lines (34 loc) · 871 Bytes
/
234.回文链表.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
#
# @lc app=leetcode.cn id=234 lang=python3
#
# [234] 回文链表
#
# %%
# @lc code=start
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reverseList(self, head):
prev, curr = None, head
post = []
while curr is not None:
curr.next, prev, curr = prev, curr, curr.next
while prev is not None:
post.append(prev.val)
prev = prev.next
return post
def isPalindrome(self, head: ListNode) -> bool:
curr = head
pre = []
while curr is not None:
pre.append(curr.val)
curr = curr.next
post = self.reverseList(head)
if pre == post:
return True
else:
return False
# @lc code=end