Go语言实现:【剑指offer】链表中倒数第k个结点

该题目来源于牛客网《剑指offer》专题。

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

Go语言实现:

type ListNode struct {
   Val  int
   Next *ListNode
}func findKthToTail(k int, node *ListNode) *ListNode {
   if node == nil {
      return nil
   }
   p := node
   q := node
   i:= 0
   for ; p != nil; i++ {
      p = p.Next
      if i >= k-1 {
         q = q.Next
      }
   }
   if i >= k-1 {
      return q
   } else {
      return nil
   }
}
原文地址:https://www.cnblogs.com/dubinyang/p/12099396.html