您的位置:首页 > Web前端

剑指offer-链表中倒数第k个结点

2017-11-24 10:25 316 查看
正确代码

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
>>>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: