-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpalindromeLinkedList.py
More file actions
34 lines (31 loc) · 925 Bytes
/
palindromeLinkedList.py
File metadata and controls
34 lines (31 loc) · 925 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
30
31
32
33
34
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/palindrome-linked-list/
# Author: Miao Zhang
# Date: 2021-01-28
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def isPalindrome(self, head: ListNode) -> bool:
slow = fast = head
while fast and fast.next:
fast = fast.next.next
slow = slow.next
if fast: slow = slow.next
slow = self.reverse(slow)
while slow:
if slow.val != head.val: return False
slow = slow.next
head = head.next
return True
def reverse(self, cur: ListNode) -> ListNode:
pre = None
while cur:
nxt = cur.next
cur.next = pre
pre = cur
cur = nxt
return pre