Skip to content

Commit 2b8efa1

Browse files
author
Jessica Yung
committed
feat(ctci-2): solution and tests for p1-3
1 parent 9bf0899 commit 2b8efa1

File tree

5 files changed

+168
-7
lines changed

5 files changed

+168
-7
lines changed

laakmann-mcdowell/2-linked-lists/1_remove_duplicates.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ def remove_duplicates(first_node):
2525
:type first_node: LinkedListNode
2626
"""
2727

28-
#
2928
set_of_values = set()
3029
previous_node = None
3130
current_node = first_node
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
"""
2+
2.2 Return Kth to Last
3+
Implement an algorithm to find the kth to last element of a singly linked list.
4+
5+
Author: Jessica Yung
6+
15 October 2016
7+
"""
8+
9+
10+
import unittest
11+
12+
13+
class LinkedListNode(object):
14+
"""Singly Linked List Node"""
15+
16+
def __init__(self, value):
17+
super(LinkedListNode, self).__init__()
18+
self.value = value
19+
self.next = None
20+
21+
22+
def solution_one(head, k):
23+
current_node = head
24+
runner = current_node
25+
for i in range(k):
26+
if runner.next is not None:
27+
runner = runner.next
28+
else:
29+
print("No kth to last element.")
30+
return
31+
while runner.next is not None:
32+
runner = runner.next
33+
current_node = current_node.next
34+
return current_node
35+
36+
37+
class Tests(unittest.TestCase):
38+
39+
def set_up_linked_list(self):
40+
a = LinkedListNode(1)
41+
b = LinkedListNode(2)
42+
c = LinkedListNode(3)
43+
d = LinkedListNode(4)
44+
e = LinkedListNode(5)
45+
f = LinkedListNode(6)
46+
g = LinkedListNode(7)
47+
a.next = b
48+
b.next = c
49+
c.next = d
50+
d.next = e
51+
e.next = f
52+
f.next = g
53+
return a
54+
55+
56+
def test_solution_one(self):
57+
a = self.set_up_linked_list()
58+
self.assertEqual(solution_one(a, 3).value, 4)
59+
self.assertEqual(solution_one(a, 0).value, 7)
60+
61+
62+
if __name__ == '__main__':
63+
unittest.main()
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""
2+
2.3 Delete Middle Node
3+
4+
Implement an algorithm to delete a node in the middle (i.e. any node but the first and last node,
5+
not necessarily the exact middle) of a singly linked list, given only access to that node.
6+
7+
EXAMPLE:
8+
Input: node c from the linked list a->b->c->d->e->f
9+
Result: nothing is returned, but the new linked list looks like a->b->d->e->f
10+
11+
"""
12+
13+
"""
14+
Thoughts: need to change b.next even though we don't have access to b.
15+
This is a singly linked list so cannot say c.prev.next = c.next etc.
16+
"""
17+
from linked_list_classes import LinkedListNode
18+
import unittest
19+
20+
def solution(input_node):
21+
# How do we check that the input node is not the first node?
22+
if input_node.next is not None:
23+
next_node = input_node.next
24+
input_node.value = next_node.value
25+
input_node.next = next_node.next
26+
return input_node
27+
28+
29+
class Tests(unittest.TestCase):
30+
31+
def test_solution(self):
32+
a = LinkedListNode(1)
33+
b = LinkedListNode(2)
34+
c = LinkedListNode(3)
35+
d = LinkedListNode(4)
36+
e = LinkedListNode(5)
37+
f = LinkedListNode(6)
38+
g = LinkedListNode(7)
39+
a.next = b
40+
b.next = c
41+
c.next = d
42+
d.next = e
43+
e.next = f
44+
f.next = g
45+
solution(e)
46+
self.assertEqual(d.next.value, 6)
47+
self.assertEqual(d.next.next, g)
48+
self.assertEqual(f.next, g)
49+
50+
if __name__ == '__main__':
51+
unittest.main()
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""
2+
2.4 Partition
3+
4+
Write code to partition a linked list around a value x, such that
5+
all nodes less than x come before all nodes greater than
6+
or equal to x. If x is contained within the list, the values of x
7+
only need to be after the elements less than x. The partition element
8+
x can appear anywhere in the 'right partition'; it does not need to
9+
appear between the left and right partitions.
10+
11+
EXAMPLE:
12+
3->5->8-5->10->2->1 [partition = 5]
13+
14+
15+
"""
16+
from linked_list_classes import LinkedListNode
17+
import unittest
18+
19+
20+
21+
class Tests(unittest.TestCase):
22+
23+
def set_up_linked_list(self):
24+
a = LinkedListNode(1)
25+
b = LinkedListNode(2)
26+
c = LinkedListNode(3)
27+
d = LinkedListNode(4)
28+
e = LinkedListNode(5)
29+
f = LinkedListNode(6)
30+
g = LinkedListNode(7)
31+
a.next = b
32+
b.next = c
33+
c.next = d
34+
d.next = e
35+
e.next = f
36+
f.next = g
37+
return a
38+
39+
40+
def test_solution_one(self):
41+
a = self.set_up_linked_list()
42+
self.assertEqual(solution_one(a, 3).value, 4)
43+
self.assertEqual(solution_one(a, 0).value, 7)
44+
45+
if __name__ == '__main__':
46+
unittest.main()

laakmann-mcdowell/2-linked-lists/linked_list_classes.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
Taken from InterviewCake August 2016
55
"""
66

7-
class LinkedListNode():
8-
"""Singly Linked List Nodes"""
9-
def __init__(self, value):
10-
# super(LinkedListNode, self).__init__()
11-
self.value = value
12-
self.next = None
7+
8+
class LinkedListNode(object):
9+
"""Singly Linked List Node"""
10+
11+
def __init__(self, value):
12+
super(LinkedListNode, self).__init__()
13+
self.value = value
14+
self.next = None
1315

1416

1517
# Build a singly linked list

0 commit comments

Comments
 (0)