-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplaying_w_linked_lists.py
More file actions
64 lines (50 loc) · 1.26 KB
/
playing_w_linked_lists.py
File metadata and controls
64 lines (50 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class Node:
def __init__(self, val, next=None):
self.val = val
self.next = next
class Linked_List:
def __init__(self):
self.node = None
self.head = self.node
def insert_front(self, data):
temp = Node(data)
temp.next = self.head
self.node = temp
self.head = self.node
def create_from_list(self, items):
self.node = None
for i in range(len(items)-1, -1, -1):
self.node = Node(items[i], self.node)
self.head = self.node
def check_linked_list_for_palindrome(items):
rev = list()
count = 0
e = items
m = items
s = items
tick = False
while e.next is not None:
count += 1
e = e.next
if tick:
m = m.next
tick = not tick
if count % 2 == 0:
if m.val != m.next.val:
return False
else:
item = m.next.next
else:
item = m.next
while item is not None:
rev.append(item.val)
item = item.next
while rev:
if rev.pop() != s.val:
return False
else:
s = s.next
return True
ll = Linked_List()
ll.create_from_list(["a","b","c","f","b","a"])
print(check_linked_list_for_palindrome(ll.head))