# -*- 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 # 返回的是一个链表节点 所以需要找到相应的指针 count = 0 p = head while (p): count += 1 p = p.next if count<k: return None tmp = count-k+1 while(tmp-1>0): head = head.next tmp = tmp-1 return head
# -*- 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 and not k: return None p1,p2 = head,head count = 1 while p2.next != None: if count >=k: print("p1 "+str(p1.val)) p1 = p1.next #print("p2 "+str(p2.val)) p2 = p2.next count += 1 if count<k: return None return p1
*- 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 prev = head needed = head step = 0 while prev: prev = prev.next if step >= k: needed = needed.next step += 1 if step < k: return None return needed
一次遍历找出倒数第K个值
使用两个指针
1.第一个向前走k-1步;第二个不动
2.第k步开始,两个指针同时向前遍历直至第一个指针到达末尾,此时第二个恰好位于倒数第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 # 判断链表是否为空 且K是否超出链表长度 if head == None or k == 0: return None pFirst = head pSecond = head for i in range(k-1): pFirst = pFirst.next if pFirst == None: return None while pFirst: pFirst = pFirst.next if pFirst: pSecond = pSecond.next return pSecond
# -*- 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 list = [] while head != None: list.append(head) head = head.next if k > len(list)&nbs***bsp;k < 1: return None return list[-k]
class Solution: def FindKthToTail(self, head, k): # write code here if k<1: return None l = [None for _ in range(k)] n = head i = 0 while n: l[i%k] = n i+=1 n = n.next if k > i: return None return l[i%k]
class Solution: def FindKthToTail(self, head, k): # write code here count = 0 p = head while p <> None: p = p.next count += 1 if count < k: return None p = head for _ in range(count-k): p = p.next return p
class Solution: def FindKthToTail(self, head, k): # write code here (742)# construct a slide area if k<= 0&nbs***bsp;head == None: return None t ,pk = head, head while t!=None: if k>0: k-=1 else: pk = pk.next # k is out t = t.next if t == None and k == 0: return pk else: (3328)# k> len(List) return None如果从头节点开始遍历到尾节点,再倒着往回数太麻烦。可以做一个长度为k的区间滑块,用两个指针定义头尾,当走在前面的指针达到尾节点时,后面的指针指的就是倒数第k个元素。注意的是:有可能k比整个链表都长,要处理一下。
# -*- coding:utf-8 -*- """ 注意: 1. 无节点 2. k为0 3. k大于节点个数 使用两个指针headBackup = head,p先走k-1步 1) 能走k-1步吗? 不能的话说明k大于节点的个数 2)可以走headBackup再跟上 """ class ListNode: def __init__(self, x): self.val = x self.next = None class Solution: def FindKthToTail(self, head, k): if k == 0: return None if head == None: return None headBackup = head cnt = 0 while(head.next != None and cnt < k-1): head = head.next cnt += 1 if cnt < k -1: return None else: while(head.next != None): head = head.next headBackup = headBackup.next return headBackup
题目描述
输入一个链表,输出该链表中倒数第k个结点。
参考一下其他大佬的思路,自己再介绍一下自己的理解。
分析思路
链表查找从头结点到尾结点查找,因此查找倒数第k个结点,需要从头结点依次查找到倒数第k个位置的结点。
但问题是我们事先不知道链表的总长度n是多少,这就导致不能明确倒数第k个结点该如何确定其边界和位置。
因此,可以采用快慢指针的方式来确定k的位置所在,其思想是:快指针fast先往前走k步,然后快慢一起走,当快指针为none的时候,慢指针slow走到了倒数第k个节点。
原理介绍如下:
假如链表数据元素如下:[15,3,2,4,5,6,7,10,0,9],其中要求解倒数第k=2个结点。
# -*- 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 slow,fast=head,head #先让快指针走k步,剩下n-k就是倒k的位置 for i in range(k): if not fast: return None fast=fast.next while fast: # 当快指针为空时,慢指针就刚到链表倒K的位置 slow=slow.next fast=fast.next return slow