剑指offer【13】- 链表中倒数第k个结点

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

 1 /*
 2 public class ListNode {
 3     int val;
 4     ListNode next = null;
 5 
 6     ListNode(int val) {
 7         this.val = val;
 8     }
 9 }*/
10 public class Solution {
11     public ListNode FindKthToTail(ListNode head,int k) {
12         
13         if(head == null){
14             return head;
15         }
16         int count = 0;
17         ListNode list = head;
18         
19         while(list != null){
20             count++;
21             list = list.next;
22         }
23         if(k > count){
24             return null;
25         }
26         
27         ListNode node = head;
28         for(int i = 0; i < count -k; i++){
29             node = node.next;
30         }
31         return node;
32 
33     }
34 }
原文地址:https://www.cnblogs.com/linliquan/p/11305243.html