Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add is_palindrome method to LinkedList class #657

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions pydatastructs/linear_data_structures/linked_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,39 @@ def popright(self):
"""
return self.extract(-1)

def is_palindrome(self) -> bool:
"""
Checks if the linked list is a palindrome.

Returns
=======

bool
True if the linked list is a palindrome, otherwise False.

Examples
========

>>> from pydatastructs import SinglyLinkedList
>>> sll = SinglyLinkedList()
>>> sll.append(1)
>>> sll.append(2)
>>> sll.append(1)
>>> sll.is_palindrome()
True
>>> sll.append(3)
>>> sll.is_palindrome()
False
"""
elements = []
current_node = self.head
while current_node is not None:
elements.append(current_node.key)
current_node = current_node.next
if current_node == self.head:
break
return elements == elements[::-1]

class DoublyLinkedList(LinkedList):
"""
Represents Doubly Linked List
Expand Down
56 changes: 56 additions & 0 deletions pydatastructs/linear_data_structures/tests/test_linked_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ def test_DoublyLinkedList():
assert str(dll_copy) == "[]"
assert raises(ValueError, lambda: dll_copy.extract(1))

dll_palindrome = DoublyLinkedList()
dll_palindrome.append(1)
dll_palindrome.append(2)
dll_palindrome.append(3)
dll_palindrome.append(2)
dll_palindrome.append(1)
assert dll_palindrome.is_palindrome() is True

dll_not_palindrome = DoublyLinkedList()
dll_not_palindrome.append(1)
dll_not_palindrome.append(2)
dll_not_palindrome.append(3)
assert dll_not_palindrome.is_palindrome() is False

def test_SinglyLinkedList():
random.seed(1000)
sll = SinglyLinkedList()
Expand Down Expand Up @@ -79,6 +93,20 @@ def test_SinglyLinkedList():
assert str(sll_copy) == "[]"
assert raises(ValueError, lambda: sll_copy.extract(1))

sll_palindrome = SinglyLinkedList()
sll_palindrome.append(1)
sll_palindrome.append(2)
sll_palindrome.append(3)
sll_palindrome.append(2)
sll_palindrome.append(1)
assert sll_palindrome.is_palindrome() is True

sll_not_palindrome = SinglyLinkedList()
sll_not_palindrome.append(1)
sll_not_palindrome.append(2)
sll_not_palindrome.append(3)
assert sll_not_palindrome.is_palindrome() is False

def test_SinglyCircularLinkedList():
random.seed(1000)
scll = SinglyCircularLinkedList()
Expand Down Expand Up @@ -116,6 +144,20 @@ def test_SinglyCircularLinkedList():
assert str(scll_copy) == "[]"
assert raises(ValueError, lambda: scll_copy.extract(1))

scll_palindrome = SinglyCircularLinkedList()
scll_palindrome.append(1)
scll_palindrome.append(2)
scll_palindrome.append(3)
scll_palindrome.append(2)
scll_palindrome.append(1)
assert scll_palindrome.is_palindrome() is True

scll_not_palindrome = SinglyCircularLinkedList()
scll_not_palindrome.append(1)
scll_not_palindrome.append(2)
scll_not_palindrome.append(3)
assert scll_not_palindrome.is_palindrome() is False

def test_DoublyCircularLinkedList():
random.seed(1000)
dcll = DoublyCircularLinkedList()
Expand Down Expand Up @@ -155,6 +197,20 @@ def test_DoublyCircularLinkedList():
assert str(dcll_copy) == "[]"
assert raises(ValueError, lambda: dcll_copy.extract(1))

dcll_palindrome = DoublyCircularLinkedList()
dcll_palindrome.append(1)
dcll_palindrome.append(2)
dcll_palindrome.append(3)
dcll_palindrome.append(2)
dcll_palindrome.append(1)
assert dcll_palindrome.is_palindrome() is True

dcll_not_palindrome = DoublyCircularLinkedList()
dcll_not_palindrome.append(1)
dcll_not_palindrome.append(2)
dcll_not_palindrome.append(3)
assert dcll_not_palindrome.is_palindrome() is False

def test_SkipList():
random.seed(0)
sl = SkipList()
Expand Down
Loading