Last active
August 29, 2015 14:03
-
-
Save habina/20fd04778919563f0126 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
============================================================================ | |
Question : 2.7 Implement a function to check if a linked list is a palindrome. | |
Solution : Save all elements in to a python list, and use slicing to check if | |
it is a palindrome | |
Time Complexity : O(N) | |
Space Complexity: O(N) | |
Gist Link : https://gist.github.com/habina/20fd04778919563f0126 | |
============================================================================ | |
""" | |
import random | |
class Node: | |
def __init__(self, value=None, next=None): | |
self.value = value | |
self.next = next | |
def __str__(self): | |
return self.value | |
def print_linkedList(node): | |
while node: | |
if(node.value != None): | |
print(node.value) | |
node = node.next | |
print() | |
def palindrome(head): | |
lst = [] | |
while(head != None): | |
lst += [head.value] | |
head = head.next | |
if(lst == lst[::-1]): | |
return True | |
else: | |
return False | |
head = Node('a', Node('b', Node('d', Node('b', Node('a', None))))) | |
print(palindrome(head)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment