Skip to content

Commit c7b247d

Browse files
author
Jessica Yung
committed
feat(ctci2.6): add function to check if singly linked list is a palindrome. add unit test for fn.
1 parent 47bf39f commit c7b247d

File tree

1 file changed

+71
-5
lines changed

1 file changed

+71
-5
lines changed

laakmann-mcdowell/2-linked-lists/6_palindrome.py

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def is_dll_palindrome(head):
2020
while runner.next is not None:
2121
runner = runner.next
2222
list_length += 1
23-
while number_checked < int(list_length / 2 + 1)
23+
while number_checked < int(list_length / 2 + 1):
2424
if current.value != runner.value:
2525
return False
2626
else:
@@ -33,6 +33,69 @@ def is_dll_palindrome(head):
3333
# If it's a singly-linked list, then we go through it once and get the length.
3434
# We may as well get all the values and put it in an array and compare?
3535

36+
def reverse_and_clone(node):
37+
prev = None
38+
while node is not None:
39+
current = LinkedListNode(node.value)
40+
current.next = prev
41+
prev = current
42+
node = node.next
43+
return prev
44+
45+
46+
def is_equal_ll(head1, head2):
47+
node1 = head1
48+
node2 = head2
49+
while node1 is not None and node2 is not None:
50+
if node1.value != node2.value:
51+
print(node1.value, node2.value)
52+
return False
53+
node1 = node1.next
54+
node2 = node2.next
55+
return True
56+
57+
58+
def is_sll_palindrome_reverse(head):
59+
reverse = reverse_and_clone(head)
60+
return is_equal_ll(head, reverse)
61+
62+
class Stack(object):
63+
def __init__(self, value):
64+
super(Stack, self).__init__()
65+
self.top = None
66+
67+
def pop(self):
68+
"""Removes and returns the top item"""
69+
removed = self.top
70+
self.top = self.top.next
71+
return removed
72+
73+
def push(self, item):
74+
"""Adds an item to the stack."""
75+
second = self.top
76+
self.top = LinkedListNode(item)
77+
self.top.next = second
78+
79+
80+
def is_sll_palindrome_runners(head):
81+
slow = head
82+
fast = head
83+
faster = head
84+
if __name__ == '__main__':
85+
while faster is not None:
86+
fast = faster
87+
# TODO: Slow runner adds node values to a stack
88+
slow_stack.add(slow)
89+
# Shift nodes
90+
slow = slow.next
91+
faster = fast.next.next
92+
# If odd number of nodes
93+
if fast.next = None:
94+
# TODO: Drop first node from stack
95+
is_equal_ll(stacknode1, slow)
96+
97+
98+
3699
class Tests(unittest.TestCase):
37100

38101
def test_sll_palindrome(self):
@@ -50,9 +113,12 @@ def test_sll_palindrome(self):
50113
d.next = e
51114
e.next = f
52115
f.next = g
53-
# Sum:
54-
h = sum_lists(a, d)
55-
print(h.value, h.next.value, h.next.next.value, h.next.next.next.value)
116+
self.assertEqual(is_sll_palindrome_reverse(a), True)
56117

57118
if __name__ == '__main__':
58-
unittest.main()
119+
unittest.main()
120+
121+
"""
122+
Mistakes:
123+
- Initially wrote 'node1 != node2' instead of `node1.value != node2.value'.
124+
"""

0 commit comments

Comments
 (0)