diff --git a/README.md b/README.md index e112b13..e2add1f 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,7 @@ Happy Open Sourcing! - [Detect_Loop_in_Linked_List](algorithms/Linked_List/detect_loop_in_linkedlist) - [Delete consecutive duplicate elements in Linked List](algorithms/Linked_List/Delete_duplicate_from_linkedlist) - [Reversing a Linked List using stack](algorithms/Linked_List/reverse_linkedlist_using_stack) +- [Check if a Linked List is Palindromic using two pointers recursively](algorithms/Linked_List/is_Linkedlist_palindrome) ### Graph diff --git a/algorithms/Linked_List/is_Linkedlist_palindrome/README.md b/algorithms/Linked_List/is_Linkedlist_palindrome/README.md new file mode 100644 index 0000000..aa9bcbe --- /dev/null +++ b/algorithms/Linked_List/is_Linkedlist_palindrome/README.md @@ -0,0 +1,41 @@ +### Check if the given linked list is palindrome or not. + +This method functions recursively using two pointers: + keep `slow` pointer on head and `cur` pointer on second last node. Check if value of slow node and last node (`cur.next`) is equal. + if equal: delete last node i.e `cur.next=None` as we dont need it any more. move `slow` pointer to next node. call `isPalindrome(slow)` recursively and it will return True when it comes to base case. + else : return False + +### Input Format: +First line of input contains number of testcases t. For each testcase, first line of input contains length of linked list N and next line contains N integers as data of linked list. + +### Output Format: +For each test case output will be 1 if the linked list is a palindrome else 0. + +### Task: +The task is to complete the function isPalindrome() which takes head as reference as the only parameter and returns true or false if linked list is palindrome or not respectively. + +### Sample Input: +``` +2 +3 +1 2 1 +4 +1 2 3 4 +``` +### Sample Output: +``` +1 +0 +``` + +### Explanation: +# Testcase 1: + 1 2 1 is palindrome +# Testcase 2: + 1 2 3 4 is not equal to its reversed Linked list 4 3 2 1 + +### Implemented in: +- [Python](isPalindrome.py) + + + diff --git a/algorithms/Linked_List/is_Linkedlist_palindrome/isPalindrome.py b/algorithms/Linked_List/is_Linkedlist_palindrome/isPalindrome.py new file mode 100644 index 0000000..126178d --- /dev/null +++ b/algorithms/Linked_List/is_Linkedlist_palindrome/isPalindrome.py @@ -0,0 +1,65 @@ +import math +import os +import random +import re +import sys + +class SinglyLinkedListNode: + def __init__(self, node_data): + self.data = node_data + self.next = None + + def __repr__(self): + return "{} -> {}".format(self.data, self.next) + +class SinglyLinkedList: + def __init__(self): + self.head = None + self.tail = None + + def insert_node(self, node_data): + node = SinglyLinkedListNode(node_data) + + if not self.head: + self.head = node + else: + self.tail.next = node + self.tail = node + +'''checks if given linked list is Palindrome or not by RECURSIVE way.''' + +def isPalindrome(head): + if (head is None) or (head.next is None): + return True #LinkedList is Palindrome if it is null or has only head node + + slow, cur = head, head + + while (cur.next.next != None): + cur = cur.next # move cur pointer to 2nd last element + + if slow.data == cur.next.data: # if 1st and last node are equal -> continue and + cur.next = None # delete last node as we don't need it anymore + if slow.next != None: + slow = slow.next # move slow pointer to next node + return isPalindrome(slow) # call function recursively to check if slow node is equivalent to current last node + + return False # if slow node and current last node aren't equal then return false + + + + +def main(): + t=int(input()) + for cases in range(t): + size = int(input()) + a = LinkedList() # create a new linked list 'a'. + nodes_a = list(map(int, input().strip().split())) + for x in nodes_a: + a.append(x) # add to the end of the list + if isPalindrome(a.head): + print(1) + else: + print(0) + +if __name__ == '__main__': + main() \ No newline at end of file