-
Notifications
You must be signed in to change notification settings - Fork 892
/
problem_026.py
63 lines (48 loc) · 1.27 KB
/
problem_026.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
class Node:
def __init__(self, x):
self.val = x
self.next = None
def __str__(self):
string = "["
node = self
while node:
string += "{} ->".format(node.val)
node = node.next
string += "None]"
return string
def get_nodes(values):
next_node = None
for value in values[::-1]:
node = Node(value)
node.next = next_node
next_node = node
return next_node
def get_list(head):
node = head
nodes = list()
while node:
nodes.append(node.val)
node = node.next
return nodes
def remove_kth_last(head, k):
if not head or not k:
return head
dummy = Node(None)
dummy.next = head
runner = head
for _ in range(k):
runner = runner.next
current_node = dummy
while runner:
runner = runner.next
current_node = current_node.next
current_node.next = current_node.next.next
return dummy.next
assert get_list(remove_kth_last(
get_nodes([]), 1)) == []
assert get_list(remove_kth_last(
get_nodes([0, 3, 7, 8, 10]), 2)) == [0, 3, 7, 10]
assert get_list(remove_kth_last(
get_nodes([7, 8, 10]), 3)) == [8, 10]
assert get_list(remove_kth_last(
get_nodes([7, 8, 10]), 1)) == [7, 8]