From 4354ba097220e0dde34daf44b92b8fa811856e60 Mon Sep 17 00:00:00 2001 From: Abhishek Khale Date: Mon, 29 Jul 2019 21:16:15 +0530 Subject: [PATCH 1/4] check if linkedList is palindrome, Recursively --- .../is_Linkedlist_palindrome/isPalindrome.py | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 algorithms/Linked_List/is_Linkedlist_palindrome/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 From 47a5492713cd9fb7819770426444a5389e49fca1 Mon Sep 17 00:00:00 2001 From: Abhishek Khale Date: Mon, 29 Jul 2019 21:47:31 +0530 Subject: [PATCH 2/4] add README file --- .../is_Linkedlist_palindrome/README.md | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 algorithms/Linked_List/is_Linkedlist_palindrome/README.md 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..90ad608 --- /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) + + + From daff3c325f60b6923cd8d8c68b3340da4c8975bf Mon Sep 17 00:00:00 2001 From: Abhishek Khale Date: Mon, 29 Jul 2019 22:05:30 +0530 Subject: [PATCH 3/4] add a new algo --- README.md | 1 + 1 file changed, 1 insertion(+) 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 From 3709b782264c629127edac1bbfb8607cbc7a101d Mon Sep 17 00:00:00 2001 From: Abhishek Khale Date: Mon, 29 Jul 2019 22:26:44 +0530 Subject: [PATCH 4/4] Upadate & add README --- algorithms/Linked_List/is_Linkedlist_palindrome/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/algorithms/Linked_List/is_Linkedlist_palindrome/README.md b/algorithms/Linked_List/is_Linkedlist_palindrome/README.md index 90ad608..aa9bcbe 100644 --- a/algorithms/Linked_List/is_Linkedlist_palindrome/README.md +++ b/algorithms/Linked_List/is_Linkedlist_palindrome/README.md @@ -1,4 +1,4 @@ -#Check if the given linked list is palindrome or not. +### 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.