LeetCode 234: Palindrome Linked List - Python (Details)

Published: 27 May 2020
on channel: HelmyCodeCamp
171
5

Patreon: https://www.patreon.com/user?u=35312452

Connect with me on Linkedin!   / helmy47  

Python Code:

-------------------------------------------------------------

class Solution:
def isPalindrome(self, head):
fast= head
slow= head
rev= None
tmp_slow= None
while fast and fast.next:
fast= fast.next.next
tmp_slow= slow.next
slow.next= rev
rev= slow
slow= tmp_slow

if fast:
slow= slow.next

while slow:
if slow.val != rev.val:
return False

slow= slow.next
rev= rev.next
return True
--------------------------------------------------------------------

Time complexity: O(N), it has to iterate through the length of the dataset N.

Space complexity: O(1), regardless of the dataset size, the space complexity is constant.


Watch video LeetCode 234: Palindrome Linked List - Python (Details) online without registration, duration hours minute second in high quality. This video was added by user HelmyCodeCamp 27 May 2020, don't forget to share it with your friends and acquaintances, it has been viewed on our site 171 once and liked it 5 people.