# class Solution:
# def duplicate(self , numbers ):
# # write code here
# ls = {}
# for i in numbers:
# if i in ls:
# ls[i] += 1
# else:
# ls[i] = 1
# for k in numbers:
# if ls[k] >= 2:
# return k
# return -1
#这个是我写的版本====一定要写上循环呀!!!!!!。。。。。
class Solution:
def FindKthToTail(self , pHead , k ):
pur,outpoint = pHead,pHead
length,count = 0,0
while pur:
pur = pur.next
length += 1
if k > length:
return None
while pHead:
if count < (length - k):
outpoint = outpoint.next
count += 1
else:
return outpoint
#这个代码不懂
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def FindKthToTail(self , pHead , k ):
if not pHead:
return pHead
if k <= 0:
node = ListNode(0)
return node.next
last_node = pHead
for i in range(k - 1):
if last_node.next != None:
last_node = last_node.next
else:
return last_node.next
while last_node.next != None:
last_node = last_node.next
pHead = pHead.next
return pHead
#这是通过的案例#
class Solution:
def FindKthToTail(self , pHead , k ):
pur,outpoint = pHead,pHead
length,count = 0,0
while pur:
pur = pur.next
length += 1
while pHead:
if count == length - k :
return pHead
pHead = pHead.next
count += 1
#这是我原来的版本
class Solution:
def FindKthToTail(self , pHead , k ):
pur,outpoint = pHead,pHead
length,count = 0,0
if pur != None:
pur = pur.next
length += 1
if count < (length - k + 1):
pHead = pHead.next
count += 1
return pHead
牛客网剑指offer-输出链表的导数第k个节点
最新推荐文章于 2021-10-11 20:27:04 发布