Skip to content

Commit 15f6dc0

Browse files
authored
Added python solution for 284. Palindrome Linked List and Renamed the file. (#133)
* Added Python solution for 284. Palindrome Linked List * Changed file name for 284.Palindrome Linked List
1 parent 35548f1 commit 15f6dc0

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution:
2+
def isPalindrome(self, head: Optional[ListNode]) -> bool:
3+
fast, slow = head, head
4+
5+
while fast != None and fast.next != None:
6+
fast = fast.next.next
7+
slow = slow.next
8+
9+
slow = self.reverse(slow)
10+
fast = head
11+
12+
while slow != None:
13+
if fast.val != slow.val:
14+
return False
15+
16+
fast = fast.next
17+
slow = slow.next
18+
19+
return True
20+
21+
def reverse(self, head):
22+
prev = None
23+
24+
while head != None:
25+
next = head.next
26+
head.next = prev
27+
prev = head
28+
head = next
29+
30+
return prev

0 commit comments

Comments
 (0)