Skip to content

Commit 6b8ed65

Browse files
2.6 done
1 parent dda3937 commit 6b8ed65

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
"""
2+
Palindrome: Implement a function to check if a linked list is a
3+
palindrome. Hints:#5, #13, #29, #61, #101
4+
"""
5+
class Node:
6+
#Singly link list
7+
def __init__(self,data = None):
8+
self.data = data
9+
self.next = None
10+
11+
class linklist:
12+
#linkList class
13+
def __init__(self):
14+
self.head = None
15+
self.size = 0
16+
17+
def push(self,data):
18+
node = Node(data)# create a new node
19+
20+
if self.head == None: #check to see if the head node is empty
21+
self.head = node # If its empty add it to the new node
22+
self.size = 1
23+
return
24+
#if the head of the linklist is filled
25+
26+
current = self.head
27+
while current.next is not None:#Check the current postion is empty
28+
#Move to the next line if nothing is there
29+
current = current.next
30+
31+
32+
current.next = node #point self.head to a new node
33+
self.size+=1
34+
35+
def lenght(self):
36+
#note the count doesn't start at zero
37+
cur = self.head
38+
counter = 0
39+
while cur is not None:
40+
counter+=1
41+
cur = cur.next
42+
print('Linklist len: '+str(counter))
43+
return counter
44+
45+
def printList(self):
46+
curr = self.head
47+
elem = []
48+
49+
while(curr != None):
50+
elem.append(curr.data)
51+
curr = curr.next
52+
print(elem)
53+
#1->2->3
54+
def remove_node(self,data):
55+
#1->2->3
56+
curr = self.head
57+
if curr is not None and curr.data == data:
58+
self.head = curr.next
59+
curr = None
60+
#look for the node that has the data we are looking for
61+
while curr is not None:
62+
if curr.data == data:
63+
break
64+
prev = curr
65+
curr = curr.next
66+
67+
#if data isn't found just reutrn
68+
if(curr == None):
69+
return
70+
71+
#allow us to unlink the nodes
72+
prev.next = curr.next
73+
curr = None
74+
75+
def palindromeCheck(self):
76+
curr = self.head
77+
str1 = ""
78+
str2 = ""
79+
while curr is not None:
80+
str1 += curr.data
81+
curr = curr.next
82+
83+
for i in range(len(str1)):
84+
str2+=str1[len(str1) - 1 - i]
85+
86+
87+
if str2 == str1:
88+
return True
89+
else:
90+
return False
91+
92+
93+
94+
#anna
95+
llist = linklist()
96+
llist.push('a')
97+
llist.push('n')
98+
llist.push('n')
99+
llist.push('a')
100+
print(llist.palindromeCheck())

0 commit comments

Comments
 (0)