Skip to content

Instantly share code, notes, and snippets.

@habina
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save habina/20fd04778919563f0126 to your computer and use it in GitHub Desktop.
Save habina/20fd04778919563f0126 to your computer and use it in GitHub Desktop.
"""
============================================================================
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