-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheasylinkedlist_234_palindromeLinkedList.py
87 lines (67 loc) · 1.74 KB
/
easylinkedlist_234_palindromeLinkedList.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"""
https://leetcode.com/problems/palindrome-linked-list/
https://www.youtube.com/watch?v=yOzXms1J6Nk&list=PLot-Xpze53lfQmTEztbgdp8ALEoydvnRQ&index=13
leetcode 234
medium
palindrome eg: racecar
input : singly linkedlist
output: determine if it is a palindrome
Logic :
approach 1 : using extra space
put into an array ,
use indices and check if palindrome
but this needs extra array
nums = []
while head :
nums.append(head.val)
head = head.next
l, r = 0, len(nums)-1
while l<= r :
if nums[l] != nums[r]:
return False
l += 1
r -= 1
return True
--
approach 2 : o(1) space
-2 pointers : slow and fast
-when fast reaches end, slow will reach the middle
-reverse linkedlist after middle
Time Complexity:
"""
# Definition for singly-linked list.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
from typing import Optional
class Solution:
def isPalindrome(self, head: Optional[ListNode]) -> bool:
fast = head #2 pointers
slow = head
# find the middle (slow)
while fast and fast.next: #till fast is at the end of list or at the last node
fast = fast.next.next
slow = slow.next
# reverse second half
prev = None
while slow:
tmp = slow.next
slow.next = prev
prev = slow
slow = tmp
# check palindrome
left, right = head, prev #prev is last
while right:
if left.val != right.val:
return False
left = left.next
right = right.next
return True
"""
testing :
Input: head = [1,2,2,1]
Output: true
Input: head = [1,2]
Output: false
"""