正确代码
class Solution:
def FindKthToTail(self, head, k):
l = []
while head!=None:
l.append(head)
head = head.next
if k > len(l) or k < 1:
return
return l[-k]
或者
class Solution:
def FindKthToTail(self, head, k):
l = []
while head is not None:
l.append(head)
head = head.next
if k > len(l) or k < 1:
return
return l[-k]
错误代码
class Solution:
def FindKthToTail(self, head, k):
l = []
while head!=None:
l.append(head)
head = head.next
if k > len(l) or k < 1:
return
return l[-k]
这就是not head 与head!=None(head is not None)的区别了。这个在博客http://blog.csdn.net/sasoritattoo/article/details/12451359中讲的很清楚,在这我就不细说。
贴一个该博客中的很好的例子
>>> x = []
>>> y = None
>>>
>>> x is None
False
>>> y is None
True
>>>
>>>
>>> not x
True
>>> not y
True
>>>
>>>
>>> not x is None
>>> True
>>> not y is None
False
>>>