Skip to content
This repository was archived by the owner on Nov 26, 2019. It is now read-only.

Is palindrome #176

Open
wants to merge 4 commits into
base: master
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
41 changes: 41 additions & 0 deletions algorithms/Linked_List/is_Linkedlist_palindrome/README.md
Original file line number Diff line number Diff line change
@@ -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)



65 changes: 65 additions & 0 deletions algorithms/Linked_List/is_Linkedlist_palindrome/isPalindrome.py
Original file line number Diff line number Diff line change
@@ -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()