-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoffer22.py
47 lines (40 loc) · 1.03 KB
/
offer22.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
# -*- coding: utf-8 -*-
"""
@author: jbl
@time: 2021/9/2 10:25
"""
from typing import List
# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
# class Solution:
# def getKthFromEnd(self, head: ListNode, k: int) -> ListNode:
# n = 1
# node = head
# while node.next:
# node = node.next
# n += 1
# i = 1
# node = head
# while i < n + 1 - k:
# node = node.next
# i += 1
# return node
# 快慢指针,fast先走k步,然后slow开始出发,当fast走到终点时,slow即为所求
class Solution:
def getKthFromEnd(self, head: ListNode, k: int) -> ListNode:
fast = head
i = 1
while i < k:
fast = fast.next
i += 1
slow = head
while fast.next:
fast = fast.next
slow = slow.next
return slow
if __name__ == '__main__':
solution = Solution()
print(solution)