-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathlinked_list_k.py
52 lines (46 loc) · 1005 Bytes
/
linked_list_k.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
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class ListNode2:
def __init__(self, x):
self.val = x
self.pre = None
class Solution:
def getKthFromEnd(self, head: ListNode, k: int) -> ListNode:
t = head
last = None
while t:
t2 = ListNode2(t)
t2.pre = last
last = t2
t = t.next
print('--- iter recv')
t = last
while t and k:
print(t.val)
t = t.pre
k = k-1
return t.val
if __name__ == '__main__':
n = None
h = None
for i in range(1,7):
t = ListNode(i)
if n:
n.next = t
n = t
if not h:
h = n
print('--- iter input')
t = h
while t:
print(t.val)
t = t.next
s = Solution()
h = s.getKthFromEnd(h, 2)
print('--- iter result')
t = h
while t:
print(t.val)
t = t.next