基于python,感觉代码过于冗余,条件判断和while太多,求大佬帮忙优化一下,感谢!
class Solution: def FindKthToTail(self, head, k): # write code here # 查看是否为空链表; 查看k值,若k为0,输出none if not head or k <= 0: return None else: p = head res = head right = 1 left = 1 # 首先将左右指针拉开k-1长度,如果链表长度小于k,则终止while循环 while right - left < k - 1 and p.next: p = p.next right += 1 # 查看循环终止是否因为链表长度不够 if right - left < k - 1: return p.next # 链表长度满足,则同时移动左右两个指针,直到右指针到头 else: while p.next: p = p.next res = res.next return res