Skip to content

Commit 7c5095d

Browse files
committed
Add is_palindrome method to LinkedList class
- Implemented `is_palindrome` method to check if a linked list is a palindrome. - The method works for all types of linked lists: SinglyLinkedList, DoublyLinkedList, SinglyCircularLinkedList, and DoublyCircularLinkedList. - Added comprehensive test cases to verify the functionality.
1 parent f34b7d6 commit 7c5095d

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

pydatastructs/linear_data_structures/linked_lists.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,39 @@ def popright(self):
206206
"""
207207
return self.extract(-1)
208208

209+
def is_palindrome(self) -> bool:
210+
"""
211+
Checks if the linked list is a palindrome.
212+
213+
Returns
214+
=======
215+
216+
bool
217+
True if the linked list is a palindrome, otherwise False.
218+
219+
Examples
220+
========
221+
222+
>>> from pydatastructs import SinglyLinkedList
223+
>>> sll = SinglyLinkedList()
224+
>>> sll.append(1)
225+
>>> sll.append(2)
226+
>>> sll.append(1)
227+
>>> sll.is_palindrome()
228+
True
229+
>>> sll.append(3)
230+
>>> sll.is_palindrome()
231+
False
232+
"""
233+
elements = []
234+
current_node = self.head
235+
while current_node is not None:
236+
elements.append(current_node.key)
237+
current_node = current_node.next
238+
if current_node == self.head:
239+
break
240+
return elements == elements[::-1]
241+
209242
class DoublyLinkedList(LinkedList):
210243
"""
211244
Represents Doubly Linked List

pydatastructs/linear_data_structures/tests/test_linked_lists.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,20 @@ def test_DoublyLinkedList():
4343
assert str(dll_copy) == "[]"
4444
assert raises(ValueError, lambda: dll_copy.extract(1))
4545

46+
dll_palindrome = DoublyLinkedList()
47+
dll_palindrome.append(1)
48+
dll_palindrome.append(2)
49+
dll_palindrome.append(3)
50+
dll_palindrome.append(2)
51+
dll_palindrome.append(1)
52+
assert dll_palindrome.is_palindrome() is True
53+
54+
dll_not_palindrome = DoublyLinkedList()
55+
dll_not_palindrome.append(1)
56+
dll_not_palindrome.append(2)
57+
dll_not_palindrome.append(3)
58+
assert dll_not_palindrome.is_palindrome() is False
59+
4660
def test_SinglyLinkedList():
4761
random.seed(1000)
4862
sll = SinglyLinkedList()
@@ -79,6 +93,20 @@ def test_SinglyLinkedList():
7993
assert str(sll_copy) == "[]"
8094
assert raises(ValueError, lambda: sll_copy.extract(1))
8195

96+
sll_palindrome = SinglyLinkedList()
97+
sll_palindrome.append(1)
98+
sll_palindrome.append(2)
99+
sll_palindrome.append(3)
100+
sll_palindrome.append(2)
101+
sll_palindrome.append(1)
102+
assert sll_palindrome.is_palindrome() is True
103+
104+
sll_not_palindrome = SinglyLinkedList()
105+
sll_not_palindrome.append(1)
106+
sll_not_palindrome.append(2)
107+
sll_not_palindrome.append(3)
108+
assert sll_not_palindrome.is_palindrome() is False
109+
82110
def test_SinglyCircularLinkedList():
83111
random.seed(1000)
84112
scll = SinglyCircularLinkedList()
@@ -116,6 +144,20 @@ def test_SinglyCircularLinkedList():
116144
assert str(scll_copy) == "[]"
117145
assert raises(ValueError, lambda: scll_copy.extract(1))
118146

147+
scll_palindrome = SinglyCircularLinkedList()
148+
scll_palindrome.append(1)
149+
scll_palindrome.append(2)
150+
scll_palindrome.append(3)
151+
scll_palindrome.append(2)
152+
scll_palindrome.append(1)
153+
assert scll_palindrome.is_palindrome() is True
154+
155+
scll_not_palindrome = SinglyCircularLinkedList()
156+
scll_not_palindrome.append(1)
157+
scll_not_palindrome.append(2)
158+
scll_not_palindrome.append(3)
159+
assert scll_not_palindrome.is_palindrome() is False
160+
119161
def test_DoublyCircularLinkedList():
120162
random.seed(1000)
121163
dcll = DoublyCircularLinkedList()
@@ -155,6 +197,20 @@ def test_DoublyCircularLinkedList():
155197
assert str(dcll_copy) == "[]"
156198
assert raises(ValueError, lambda: dcll_copy.extract(1))
157199

200+
dcll_palindrome = DoublyCircularLinkedList()
201+
dcll_palindrome.append(1)
202+
dcll_palindrome.append(2)
203+
dcll_palindrome.append(3)
204+
dcll_palindrome.append(2)
205+
dcll_palindrome.append(1)
206+
assert dcll_palindrome.is_palindrome() is True
207+
208+
dcll_not_palindrome = DoublyCircularLinkedList()
209+
dcll_not_palindrome.append(1)
210+
dcll_not_palindrome.append(2)
211+
dcll_not_palindrome.append(3)
212+
assert dcll_not_palindrome.is_palindrome() is False
213+
158214
def test_SkipList():
159215
random.seed(0)
160216
sl = SkipList()

0 commit comments

Comments
 (0)