Skip to content

Commit 4e8a308

Browse files
solves palindrome linked list
1 parent dcd207e commit 4e8a308

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
| 226 | [Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/InvertBinaryTree.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/invert_binary_tree.py) |
7070
| 231 | [Power of Two](https://leetcode.com/problems/power-of-two) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/PowerOf2.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/is_power_of_2.py) |
7171
| 232 | [Implement Queue Using Stacks](https://leetcode.com/problems/implement-queue-using-stacks) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/MyQueue.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/implement_queue_using_stacks.py) |
72-
| 234 | [Palindrome Linked Lists](https://leetcode.com/problems/palindrome-linked-list) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/PalindromeLinkedList.java) |
72+
| 234 | [Palindrome Linked Lists](https://leetcode.com/problems/palindrome-linked-list) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/PalindromeLinkedList.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/palindrome_linked_list.py) |
7373
| 235 | [Lowest Common Ancestor of a Binary Search Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/LowestCommonAncestorOfBinarySearchTree.java) |
7474
| 237 | [Delete a Node In A Linked List](https://leetcode.com/problems/delete-node-in-a-linked-list) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/DeleteANodeInLinkedList.java) |
7575
| 242 | [Valid Anagram](https://leetcode.com/problems/valid-anagram) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/ValidAnagram.java) |

python/palindrome_linked_list.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Definition for singly-linked list.
2+
from typing import Optional
3+
4+
5+
class ListNode:
6+
def __init__(self, val=0, next=None):
7+
self.val = val
8+
self.next = next
9+
10+
11+
class Solution:
12+
def length(self, head: ListNode) -> int:
13+
count = 0
14+
while head is not None:
15+
count += 1
16+
head = head.next
17+
return count
18+
19+
def reverse(self, head: ListNode) -> Optional[ListNode]:
20+
if head is None or head.next is None:
21+
return head
22+
a, b, c = head, head.next, head.next.next
23+
a.next = None
24+
while c is not None:
25+
b.next = a
26+
a, b, c = b, c, c.next
27+
b.next = a
28+
return b
29+
30+
def isPalindrome(self, head: ListNode) -> bool:
31+
list_length = self.length(head)
32+
first_half = head
33+
second_half = head
34+
for i in range((list_length + 1) // 2):
35+
second_half = second_half.next
36+
second_half = self.reverse(second_half)
37+
while second_half is not None:
38+
if first_half.val != second_half.val:
39+
return False
40+
second_half = second_half.next
41+
first_half = first_half.next
42+
return True

0 commit comments

Comments
 (0)