题目描述
输入一个链表,输出该链表中倒数第k个结点。
题目地址
思路
三个特例:如果输入的链表为空;k大于链表的长度;k为0的情况。对于正常情况,设置两个指针分别指向头结点,第一个指针向前走k-1步,走到正数第k个结点,同时保持第二个指针不动,然后第一个指针和第二个指针每次同时前移一步,这样第一个指针指向尾结点的时候,第二个指针指向倒数第k个结点。判断尾结点的条件是 p.next == None。
Python
# -*- coding:utf-8 -*- class ListNode: def __init__(self, x): self.val = x self.next = None # 单向链表链表 node1: 1->2->3 node1 = ListNode(1) node2 = ListNode(2) node3 = ListNode(3) node4 = ListNode(4) node1.next = node2 node2.next = node3 node3.next = node4 class Solution: def FindKthToTail(self, head, k): if not head or k <= 0: # 链表为空或k小于等于0 return None p = head q = head for i in range(1,k): if p.next == None: # k大于链表的长度 return None else: p = p.next while p.next: p = p.next q = q.next return q if __name__ == ‘__main__‘: originList = node1 print(‘链表:‘,end = ‘ ‘) while originList: print(originList.val, end = ‘ ‘) originList = originList.next k = 5 result = Solution().FindKthToTail(node1,k) print(‘\n倒数第{0}个结点:‘.format(k),end = ‘ ‘) while result: print(result.val,end = ‘ ‘) result = result.next
推广: 寻找中间节点, 两个指针一起, 第一个指针每次走两步, 第二个指针每次走一步, 快指针指到尾部, 慢指针正好指到中间
def FindMidNode(self,head): """ 推广: 寻找中间节点, 两个指针一起, 第一个指针每次走两步, 第二个指针每次走一步, 快指针指到尾部, 慢指针正好指到中间 """ if not head: return None p = head q = head p = p.next while p: p = p.next if p: p = p.next q = q.next return q mid = Solution().FindMidNode(node1) print(‘\n中间结点:‘,end = ‘ ‘) while mid: print(mid.val, end = ‘ ‘) mid = mid.next
原文地址:https://www.cnblogs.com/huangqiancun/p/9782576.html
时间: 2024-10-28 21:25:46