-
Notifications
You must be signed in to change notification settings - Fork 19
/
2.2-Return_Kth_To_Last.py
68 lines (53 loc) · 1.65 KB
/
2.2-Return_Kth_To_Last.py
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
64
65
66
67
68
# CTCI 2.2
# Remove Kth to Last
import unittest
#from LinkedList import LinkedList
# My Solution
def kth_to_last(head, k):
end = curr = head
index = curr_idx = 0
# Iterate through ll
while end is not None:
end = end.next
index += 1
# Find index (-1 because it is kth to end. e.g. 2nd to last should only be -1)
#if k != 0 and k != 1:
# index -= (k - 1)
# Or maybe I'm thinking too much
index -= k
if index < 0:
return None
while curr is not None:
if curr_idx == index:
return curr
curr = curr.next
curr_idx += 1
#-------------------------------------------------------------------------------
# CTCI Solution
# Double pointer!
def akth_to_last(head, k):
runner = current = head
for i in range(k):
if runner is None:
return None
runner = runner.next
while runner:
current = current.next
runner = runner.next
return current
#-------------------------------------------------------------------------------
#Testing
class Node():
def __init__(self, data, next=None):
self.data, self.next = data, next
class Test(unittest.TestCase):
def test_kth_to_last(self):
head = Node(1,Node(2,Node(3,Node(4,Node(5,Node(6,Node(7)))))))
self.assertEqual(None, kth_to_last(head, 0));
self.assertEqual(7, kth_to_last(head, 1).data);
self.assertEqual(4, kth_to_last(head, 4).data);
self.assertEqual(2, kth_to_last(head, 6).data);
self.assertEqual(1, kth_to_last(head, 7).data);
self.assertEqual(None, kth_to_last(head, 8));
if __name__ == "__main__":
unittest.main()