diff --git a/pydatastructs/linear_data_structures/linked_lists.py b/pydatastructs/linear_data_structures/linked_lists.py index 09178daf..132e6771 100644 --- a/pydatastructs/linear_data_structures/linked_lists.py +++ b/pydatastructs/linear_data_structures/linked_lists.py @@ -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 diff --git a/pydatastructs/linear_data_structures/tests/test_linked_lists.py b/pydatastructs/linear_data_structures/tests/test_linked_lists.py index b7f172dd..ac99200b 100644 --- a/pydatastructs/linear_data_structures/tests/test_linked_lists.py +++ b/pydatastructs/linear_data_structures/tests/test_linked_lists.py @@ -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() @@ -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() @@ -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() @@ -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()