题目描述

输入一个链表,输出该链表中倒数第k个结点

第一种实现:

# -*- coding:utf-8 -*-
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
def FindKthToTail(self, head, k):
# write code here
l = []
while head != None:
l.append(head)
head = head.next
if k > len(l) or k < 1:
return
return l[-k]

第二种实现:

# -*- coding:utf-8 -*-
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
def FindKthToTail(self, head, k):
# write code here
if head == None or k == 0:
return None
pAhead = head
pBehind = None
for i in range(0,k-1):
if pAhead.next != None:
pAhead = pAhead.next
else:
return None
pBehind = head
while pAhead.next != None:
pAhead = pAhead.next
pBehind = pBehind.next
return pBehind

最新文章

  1. tnt_esri.dat Arcgis8.1安装license
  2. jQuery学习之:Validation表单验证插件
  3. Sqoop使用手册
  4. 广义表 Head Tail
  5. 通过IP的方式建立PSSession
  6. js动态生成JSON树
  7. wx.Frame
  8. magento 获取attribute的所有option
  9. 这几天有django和python做了一个多用户博客系统(可选择模板)
  10. vue数据请求
  11. 一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](五)
  12. cogs 619. [金陵中学2007] 传话
  13. Redis出现多线程调用时抛出 [B cannot be cast to java.lang.Long] 异常
  14. 导出表结构sql语句
  15. Caused by: java.net.BindException: Address already in use: bind
  16. 评价指标1--F1值和MSE
  17. 为订阅内虚拟机批量安装并配置 Microsoft Anti-Malware 扩展
  18. SaltStack的配置管理--jinja (七)
  19. servlet request getQueryString 汉字的URI编码如何转码
  20. csharp: QR Code Barcode

热门文章

  1. 二维偏序 tree
  2. Mysql-2-数据库基础
  3. pod 安装
  4. web版聊天框
  5. es6的箭头函数转换为普通函数,以及将await/async函数转为普通函数
  6. 洛谷P1967 货车运输
  7. 跳坑 小程序swiper组件 轮播图片 右边空白问题
  8. springboot 简单使用shiro登录
  9. [题解](优先队列广搜)POJ_3635_Full Tank
  10. MacOS下,Python2和Python3完美兼容使用(转)