题目描述
输入一个链表,输出该链表中倒数第k个结点。
class Solution:
def FindKthToTail(self, head, k):
# 12345
front_node = head
behind_node = head
# 2 front_node = 2
for i in range(k):
if front_node == None:
return None
front_node = front_node.next
while front_node:
front_node = front_node.next
behind_node = behind_node.next
return behind_node
本文介绍了一种高效查找链表中倒数第K个节点的算法。通过双指针技巧,首先让前指针向前移动K步,然后两个指针同步移动直至前指针到达链表尾部,此时后指针所指向的即为所求节点。
1098

被折叠的 条评论
为什么被折叠?



